0

I have this code here in my ajax:

var userCheckbox = $("input[name=chk]:checked").val();

and this is the checkbox in my html:

<input id="checkbx" type="checkbox" name="chk" value="apple"/>apple</td>
<input id="checkbx" type="checkbox" name="chk" value="corn"/>corn</td>
<input id="checkbx" type="checkbox" name="chk" value="tomato"/>tomato</td>
<input id="checkbx" type="checkbox" name="chk" value="juice"/>juice</td>

but I'm only getting one value. how can I get multiple values? someone said to me that I should use a loop statement but I don't know where to put it.

can anyone please help. thanks in advance.

1 Answer 1

4

Use .map() to create an array of the values:

var userCheckbox = $("input[name=chk]:checked").map(function() {
    return this.value;
}).get();

Also, your ID's are repeating, remember, ID's must be unique.

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

4 Comments

I'm getting "Array" text when displayed.
Well, its an array of values.
I mean I'm not getting the values of the checkboxes.
Try logging userCheckbox[0] -- is there a value there?

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.