4

Hi I have the following page:

<input type="checkbox"  name="fruit1" id="1"  class="box">Banana<br /><br />
<input type="checkbox"  name="fruit2" id="2"  class="box">Cherry<br /><br />
<input type="checkbox"  name="fruit3" id="3"  class="box">Strawberry<br /><br />
<input type="checkbox"  name="fruit4" id="4"  class="box">Orange<br /><br />
<input type="checkbox"  name="fruit5" id="5"  class="box">Peach<br /><br />
<input type="button" id="groupdelete" value="clickme"><br />

 $(document).ready(function(){

$('#groupdelete').on('click', function(){
  var names = [];
   $('input:checked').each(function() {

       names.push($('input:checked').attr("name") + $('input:checked').attr('id'));

 });
   console.log(names); 
})

})

What I am trying to do is the following:

To add the checked checkboxes in the array. And after that, I would like to be able to pass the value in php variable.

When I excecute the code now, I am getting result like this:

["fruit22", "fruit22", "fruit22"]

Any help will be deeply appreciated.

Regards, Zoreli

2 Answers 2

12

You need to use this rather than 'input:checked' inside the .each() function to refer to the current element in the set being examined. If you re-use the selector you're getting the set again, then only ever getting the attributes from the first element in the set.

$('input:checked').each(function() {
    names.push($(this).attr("name") + this.id);
});
Sign up to request clarification or add additional context in comments.

4 Comments

Great, works like a charm...can you please tell me how could I pass this names array in php variable?
He can also use this.name instead of casting this back into a jQuery object. prop is preferred over attr as of jQuery 1.7+
And how to pass the value of names in php variable? Any idea?
Place 'names' into the value of a hidden input field and then read that from PHP when the form posts
1

Change your html to

<input type="checkbox"  name="fruits[]" id="1"  class="box">Banana<br /><br />
<input type="checkbox"  name="fruits[]" id="2"  class="box">Cherry<br /><br />
<input type="checkbox"  name="fruits[]" id="3"  class="box">Strawberry<br /><br />
<input type="checkbox"  name="fruits[]" id="4"  class="box">Orange<br /><br />
<input type="checkbox"  name="fruits[]" id="5"  class="box">Peach<br /><br />
<input type="button" id="groupdelete" value="clickme"><br />

And now, look to jQuery/Javascript

$(document).ready(function(){
    $('#groupdelete').click(function() {
        var marked = new Array();
        var k = 0;
        $('input:checked').each(function(index,value) {
            marked[k] = value;
            k++;
        });        
        alert(marked[0].id);
    });
});

alert is just giving you the demo by accessing the direct access on the array's index.

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.