1

Consider a simple each loop to inspect checked checkboxes:

$(':checkbox').filter(':checked').each(function(){
    var name = $(this).val();
});

This code works for checkboxes checked in the original HTML code as

<input type="checkbox" name="test" value="test" checked="checked" />

How to make this jQuery code to work live, to perform the filter process every time a new box is checked or a checked is removed. I want to run the each loop upon any change in the checkbox (checked or unchecked) to return the updated values of currently checked ones.

NOTE: This is a simplified code to express the issue. The intention is not to record the name, but processing checked values in the each loop.

3
  • Nobody follows the "each" tag. EDIT: WAIT, there are two guys following it. My bad. Commented May 20, 2012 at 1:04
  • @Aerovistae why not, each loop is quite useful ;) Commented May 20, 2012 at 1:06
  • @Aerovistae. We (jquery users) have that jquery-each tag... EDITED. anyway the tags are not just for the followers. Commented May 20, 2012 at 1:24

5 Answers 5

2

Use click:

$(':checkbox').click(function() {
    $(':checkbox').filter(':checked').each(function() {
        var name = this.value;
    });
});

Notes:

  • You can avoid query the DOM so many times.
  • :checkbox is a very slow selector, because it's not a css selector but a jQuery extension.
  • Don't use jQuery to get this value.

You can improve your code like that:

var $checkboxes = $('input[type="checkbox"]');
$checkboxes.filter(':checked').each(function() {
    var name = this.value;
});

Reading resources:

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

2 Comments

I really like the way you answer, I learned a lot. Don't worry about name, it is a long process with checked values. It is very simplified code to discuss the issue. I deeply appreciate your concern about performance, what most of people simply ignore :) I wish I could give +10 for this!
@Ali. I'm glad I could help. I added reading resources to the answers. Good luck.
1

You would need to bind your loop to the change event for the checkboxes.

$(':checkbox').change(function() {
    $(':checkbox').filter(':checked').each(function() {
        var name = $(this).val();
    });
});

3 Comments

it works perfectly, but has one problem. It will start to work upon first change. I mean it will not work for default checked values (those checked in the original html code).
@Ali. Then use click instead. I posted it as an answer...
@Ali, just run the inner loop once the page is loaded, either before or after you bind it. Better yet, put the inner loop in a separate function so that you'd just call it once when the page is loaded, and bind it to the event.
1

Since you're performing a loop, I'm assuming there can be many checked boxes. I'm a little confused why you would be overwriting the name variable each time though, leaving you only with the value of the last checkbox in the end. Instead, I'm providing an array which we push all checked values onto:

// Declare a names variable for storing values
var names;

// Any time a checkbox changes on our form
$("form").on("change", ":checkbox", function(e){
  // Empty the names array
  names = [];
  // Get all checked checkboxes from our form and add their value to our array
  $(":checkbox:checked", e.delegateTarget).each(function(){
    names.push( $(this).val() ); 
  });
}).find("input:checkbox").trigger("change");

// Method of revealing what we currently have checked
$("#reveal").on("click", function(){
  alert( names.join(", ") );
});

Demo: http://jsbin.com/uyecux/2/edit

2 Comments

As I added to a NOTE, name was a simplified example to complete the loop. The actual process is quite long. Thanks for your attention.
Hey Jonathan, you could use .map instead of pushing elements to array. Do you want me to give an example?
0

You need to create an event handler. In jQuery, this might look like this:

$(':checkbox').on('change', function() {
  // Execute your code here.
});

See the jQuery documentation here: http://api.jquery.com/on/ for more details on event handling in jQuery.

Comments

0
var $checkboxes = $('input[type=checkbox]')  
$checkboxes.click(function (){  
    $(':checkbox').filter(':checked').each(function(){
        var name = $(this).val();
    });
});  

Try using above 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.