0

I have check boxes inside a form (id='submit_slides_1'):

After submitting the form I want to fetch all of the values of the checked checkboxes:

<input type='checkbox' class='chk_selection_info_box' name='thumb_selection[]' value='" + images[i]['url'] + "'>

and in my javascript I have the following code:

$("#submit_slides_1").submit(function(e) {
    e.preventDefault();
    var x=$("#thumb_selection").is(":checked");
    alert(x);
  }
);

But it keeps giving me False; Am I missing something?

ADDED: Tried but it says "undefined" too:

$(".chk_selection_info_box:checked").each(function(i,l)
{
    alert(l.value);
});

If you need more clarification, please let me know which part you need more clarification!

Thanks

7
  • show us your full html Commented Nov 11, 2013 at 21:07
  • 1
    You say the form has the name of 'submit_slides_1', but you're selecting the form with the id of "#submit_slides_1". This is obvious, but not necessarily correct; please: show your HTML, don't describe it at us. Commented Nov 11, 2013 at 21:07
  • @David: No thei d is 'submit_slides_1'; my bad! still not working! Commented Nov 11, 2013 at 21:11
  • I think what you need is $('[name="thumb_selection[]"]:checked').map(function(){return this.value}); will return the array of values of the checked check boxes Commented Nov 11, 2013 at 21:13
  • .is() returns a boolean representing whether the condition is true or false. Move the :checked pseudo class to your selector $('#thumb_selection:checked') Commented Nov 11, 2013 at 21:14

3 Answers 3

3

There is no element with that id. That would be what you are missing. Add the id.

BUT your code suggests you have multiple checkboxes, if that is the case, setting an id will not work and using a class would not work since is() will only work off the first element.

var checkedElements = $(".chk_selection_info_box:checked");

You than would need to use .serialize(), .map(), or .each() to get all the values.

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

3 Comments

Using an ID won't work if she wants the checked status from multiple checkboxes.
thats true: multiple checkboxes: even the following code does not work: $(".chk_selection_info_box:checked").each(function() { alert($this.val()); }); says this undefined? Why?
Because there is no $this, there is $(this)
1

If they all have the .chk_selection_info_box class, then select those elements with the :checked selector, and .map() the values to a collection, and convert it .toArray().

var values = $(".chk_selection_info_box:checked").map(function(i, el) {
    return el.value
}).toArray();

5 Comments

You solved my problem; but why my code does not work! I didnt have any cluew about maps!
@Pasargad: The is() method returns a boolean value that tells you if the element matches the selector. The .map() simply takes the collection, and creates a new collection with the return values of the callback function. So we selected all the boxes that are checked, and returned the .value of each box to the new Array.
It meakes sense ti me now; could you please let me know a solution with each loop as well! I guess this one should work too, but apparently not: $("#thumb_selection:checked").each(function() { alert($this.val()); });
I am sorry to let you know your solution just return the very last chechbox selected!
@Pasargad: No it doesn't. It will return all the values for all the checked checkboxes with that class. Do all your boxes have that class? The code in your comment above will only return one result because you can only have one ID on the page. The answer you accepted uses the same selector I'm using, but I've shown how to get the values.
-2

Try this mate

var x = $(".chk_selection_info_box").prop('checked');

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.