Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.is(':checked') returns always true

Tags:

jquery

HTML:

<table id="list">
   <tr>
      <td><input type="checkbox" checked="true"></td>
      <td>...</td>
   </tr>
   <tr>
      <td><input type="checkbox" checked="true"></td>
      ...
   </tr>
   ...
</table>

JS:

$('#list tr').each(function(){
   console.log(
        $(this).find('input[type="checkbox"]').is(':checked')
   );    
});

Problem: the above log statement always returns true. Each tr holds some data and I want to perform an action using this data. But the user can deselect some entities which in this case is not possible because is(':checked') always returns true, even if the entry is deselected/unchecked.

Any ideas how to fix this?

like image 840
UpCat Avatar asked Oct 30 '25 20:10

UpCat


1 Answers

Your 'checked' attribute has true as its value, so it will return true as even having <input type="checkbox" checked /> will also going be checked.

I created a demo with your code, with additional function to retrieve .is(":checked") as it's property changed.

$("input").change(function() {
    console.log($(this).is(':checked'));
});

And It shows the change. Your problem must be somewhere else.

like image 95
Starx Avatar answered Nov 05 '25 07:11

Starx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!