0

I'm using an array to add data to my database, and JavaScript to show and hide field depending on yes and no.

<input type="radio" id="medication1" name="medication[]" value="Yes" />
<input type="radio" id="medication2" name="medication[]" value="No" />


 $("#show").hide();
        $("input[name=medication]").click(function()
        {
            if ($("#medication1").attr('checked'))
                $("#show2").hide();
            if ($("#medication2").attr('checked'))
                $("#show2").show();
        });

Without the array block quotes it works perfectly but once I add them it doesn't. Is there maybe a way I can get around this?

1
  • 3
    If it works without the [], why don't you just use it that way (I mean, in the form too!)? As this is a radio button, it won't ever have more than one selected... (also, what would be the semantics associated with having medication = ['Yes', 'No']?) Commented Mar 7, 2013 at 10:11

3 Answers 3

3

You need escape those special meanings chars: []:

$("input[name=medication\\[\\]]").click(function()

Source

Sign up to request clarification or add additional context in comments.

2 Comments

using $('input[name="medication[]"]')
@rab, that's a valid way as well.
1

try this with ^= in your selector you tell to select all input with name that start with medication

$("#show").hide();
        $("input[name^=medication]").click(function()
        {
            if ($("#medication1").attr('checked'))
                $("#show2").hide();
            if ($("#medication2").attr('checked'))
                $("#show2").show();
        });

Comments

0

change

$("input[name=medication]").click(function()

to

$("input[name='medication[]']").click(function()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.