0

How to pass this kind of HTML input to JavaScript so that it recognizes these array values?

<input type="checkbox" id="collection[]" value="0">
<input type="checkbox" id="collection[]" value="1">
<input type="checkbox" id="collection[]" value="2">]

The only idea I have (never played with js this way, tbh) is to reach it through:

$("#collection").val();

But I got undefined error. I have no other idea how to make javascript recognize that variable collection is an array and has to passed as such.

Thanks in advance!

4
  • What is it that you are trying to do? Decide which checkboxes are checked? Commented Jun 15, 2014 at 15:07
  • Sorry, didn't change name to id. Doesn't work either. @taylorcressy: I'm trying to pass values of selected checkboxes to json and then, through jQuery, to a php file so that only selected checkboxes are processed. But I need javascript to recognize which checkboxes to pass... Commented Jun 15, 2014 at 15:08
  • 2
    You cannot have duplicate IDs, change it to name Commented Jun 15, 2014 at 15:11
  • 1
    See stackoverflow.com/questions/19529443/… Commented Jun 15, 2014 at 15:12

3 Answers 3

1

Remember, IDs need to be unique within your document. So set by 'name' not by id.

You can use

$('#someid').is(":checked");

for individually checking each checkbox, or loop through them with a jQuery selector

To loop through them set

<input type="checkbox" name="checkboxes" value="0">
<input type="checkbox" name="checkboxes" value="1">
<input type="checkbox" name="checkboxes" value="2">

Then with jQuery,

$('input[name=checkboxes]:checked').each(function(i, e) {
        e.val();   //The value of the checkbox that is selected
});
Sign up to request clarification or add additional context in comments.

Comments

1

You cannot have Duplicate Ids. Though duplicate IDs will give you desired output in this case, it is invalid to use them for multiple elements.

<input type="checkbox" name="collection[]" value="0">
<input type="checkbox" name="collection[]" value="1">
<input type="checkbox" name="collection[]" value="2">

There are many ways you can access array based elements.

jQuery .map(): Alternative is .each()

Demo

$("[name='collection[]']").map(function(){return $(this).val();}).get()

Working Demo for checking checked inputs. To get the checked checkbox,

$('input').change(function () {
    console.log($("[name='collection[]']:checked").map(function () {
        return $(this).val();
    }).get());
});

Comments

0
$("#collection")

It mean's ,"Find me an element which id's equal collection on page" , of course it can't find anything. You can use .each function and you can use checkboxes attributes. For example ;

var myCheckBoxArray = []

$("input[type='checkbox']").each(function(i,elm){
    myCheckBoxArray.push(elm);
});

1 Comment

He only wants the checkboxes that are checked

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.