2

I am trying to build a script which checks if a checkbox is checked using jQuery $each function.Below is the HTML code:

 <tr>
  <td class="categories">World</td>
  <td class="category_enabled" style="float: right;">
  <input type="checkbox"/>
  </td>
 </tr>

 <tr>
  <td class="categories">Economy</td>
  <td class="category_enabled" style="float: right;">
  <input type="checkbox"/>
  </td>                            
 </tr>

 <tr>
   <td class="categories">Ψυχαγωγία</td>
   <td class="category_enabled" style="float: right;">
   <input type="checkbox"/>
   </td>
 </tr>

I believe it is something like (inside .each()):

if ($("find_specific_checkbox").is(":checked")){
   console.log("Checked!");
}

Any ideas how I can do this. I am trying a couple of hours but nothing yet.

GeoDim
  • 171
  • 2
  • 3
  • 11

2 Answers2

7

Based on your question and markup, I believe that you intend to iterate through each table row element, and get the status of the checkbox in each row.

If that is the case, I have created both (1) a code sample and (2) a proof-of-concept example below that does what you want to do.

Code sample

$('table tr').each(function(i) {
    // Cache checkbox selector
    var $chkbox = $(this).find('input[type="checkbox"]');

    // Only check rows that contain a checkbox
    if($chkbox.length) {
    var status = $chkbox.prop('checked');
        console.log('Table row '+i+' contains a checkbox with a checked status of: '+status);
    }
});

Working example

I have set the first checkbox to a checked status, so you can see how the logic works.

$(function() {
  // General/modular function for status logging
  var checkboxChecker = function() {
    $('table tr').each(function(i) {
      // Only check rows that contain a checkbox
      var $chkbox = $(this).find('input[type="checkbox"]');
      if ($chkbox.length) {
        var status = $chkbox.prop('checked');
        console.log('Table row ' + i + ' contains a checkbox with a checked status of: ' + status);
      }
    });
  };
  
  // Check checkboxes status on DOMready
  checkboxChecker();
  
  // Check again when checkboxes states are changed
  $('table tr input[type="checkbox"]').on('change', function() {
    checkboxChecker();
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td class="categories">World</td>
    <td class="category_enabled" style="float: right;">
      <input type="checkbox" checked />
    </td>
  </tr>

  <tr>
    <td class="categories">Economy</td>
    <td class="category_enabled" style="float: right;">
      <input type="checkbox" />
    </td>
  </tr>

  <tr>
    <td class="categories">Ψυχαγωγία</td>
    <td class="category_enabled" style="float: right;">
      <input type="checkbox" />
    </td>
  </tr>
</table>
Terry
  • 63,248
  • 15
  • 96
  • 118
6

You have to loop through every checkbox in that column and then check whether it is checked or not.

$(document).ready(function() {
  $('table [type="checkbox"]').each(function(i, chk) {
    if (chk.checked) {
      console.log("Checked!", i, chk);
    }
  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<table>
  <tr>
    <td class="categories">World</td>
    <td class="category_enabled" style="float: right;">
      <input type="checkbox" />
    </td>
  </tr>

  <tr>
    <td class="categories">Economy</td>
    <td class="category_enabled" style="float: right;">
      <input type="checkbox" checked />
    </td>
  </tr>

  <tr>
    <td class="categories">Ψυχαγωγία</td>
    <td class="category_enabled" style="float: right;">
      <input type="checkbox" />
    </td>
  </tr>
</table>
Sankar
  • 6,908
  • 2
  • 30
  • 53