javascript - jQuery : Counting checked checkboxes - wrong count -


i have code counts checked checkboxes. works fine when selecting, count breaks (adds +1 count) when deselect of selected checkboxes.

html:

<div>number of checkboxes checked: <span id='chck'>0</span></div> <table>     <tr><td><input type='checkbox' /></td></tr>      <tr><td><input type='checkbox' /></td></tr>      <tr><td><input type='checkbox' /></td></tr>      <tr><td><input type='checkbox' /></td></tr>      <tr><td><input type='checkbox' /></td></tr>      <tr><td><input type='checkbox' /></td></tr>      <tr><td><input type='checkbox' /></td></tr>     </table> 

js:

$('tr :checkbox').change(function(){      $('#chck').text($(this).add(':checked').length);  }); 

fiddle: http://jsfiddle.net/kasxj/

http://jsfiddle.net/kasxj/3/

not sure why you're using $(this).add(':checked') when $(':checked') suffice.


Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

c++ - Correct method for redrawing a layered window -

java.util.scanner - How to read and add only numbers to array from a text file -