19

I have an HTML table with a checkbox in each row.
I want to loop over the table and see if there are any checkboxes that are checked.
The following does not work:

$("#save").click( function() {
    $('#mytable tr').each(function (i, row) {
        var $actualrow = $(row);
        checkbox = $actualrow.find('input:checked');
        console.log($checkbox);
});

This prints in the console the following:

[prevObject: jQuery.fn.jQuery.init[1], context: tr, selector: "input:checked", constructor: function, init: function…]

per row regardless of whether any checkbox is checked.

Update
Same issue with:

$('#mytable tr').each(function (i, row) {                                                                                                 
   var $actualrow = $(row);
    $checkbox = $actualrow.find(':checkbox:checked');
    console.log($checkbox);  
});
6
  • 3
    Yup, a jQuery object is printed to the console, which is normal. Did you check its length property? Commented Aug 20, 2013 at 9:51
  • 1
    Have you tried with $actualrow.find('input').is(':checked'); Commented Aug 20, 2013 at 9:52
  • log $checkbox.length. Is the length non zero? Commented Aug 20, 2013 at 9:52
  • @FredericHamidi:Length is 0 Commented Aug 20, 2013 at 10:09
  • 1
    @Jim, there you go. It means no checkbox was checked for that row. As for how to use it, you could write an if statement. Commented Aug 20, 2013 at 10:11

3 Answers 3

63

Use this instead:

$('#save').click(function () {
    $('#mytable').find('input[type="checkbox"]:checked') //...
});

Let me explain you what the selector does: input[type="checkbox"] means that this will match each <input /> with type attribute type equals to checkbox After that: :checked will match all checked checkboxes.

You can loop over these checkboxes with:

$('#save').click(function () {
    $('#mytable').find('input[type="checkbox"]:checked').each(function () {
       //this is the current checkbox
    });
});

Here is demo in JSFiddle.


And here is a demo which solves exactly your problem http://jsfiddle.net/DuE8K/1/.

$('#save').click(function () {
    $('#mytable').find('tr').each(function () {
        var row = $(this);
        if (row.find('input[type="checkbox"]').is(':checked') &&
            row.find('textarea').val().length <= 0) {
            alert('You must fill the text area!');
        }
    });
});
Sign up to request clarification or add additional context in comments.

7 Comments

I want to get the individually.If a checkbox is checked but the textbox in the same row is not filled I want to show an alert
Ok, but how do I get the textbox in the next column of the same row to check if it is populated and if not show an alert?
Check this jsfiddle.net/DuE8K/2. Do you see the variables textarea and checkbox? They are referencing to the textarea and checkbox located in the current row.
Works like a charm!Is it too much to ask how could I make the coresponding row red besided the alert?
|
2

use .filter(':has(:checkbox:checked)' ie:

$('#mytable tr').filter(':has(:checkbox:checked)').each(function() {
 $('#out').append(this.id);
});

Comments

1
The following code snippet enables/disables a button depending on whether at least one checkbox on the page has been checked.
$('input[type=checkbox]').change(function () {
    $('#test > tbody  tr').each(function () {
        if ($('input[type=checkbox]').is(':checked')) {
            $('#btnexcellSelect').removeAttr('disabled');
        } else {
            $('#btnexcellSelect').attr('disabled', 'disabled');
        }
        if ($(this).is(':checked')){
            console.log( $(this).attr('id'));
         }else{
             console.log($(this).attr('id'));
         }
     });
});

Here is demo in JSFiddle.

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.