-1

I have a table and in each column it has a checkbox which has a class but no id or name. When the checkbox is checked or unchecked I am showing the checkbox state using an alert():

$(document).on('change', '[class*="box"]', function () {
    var tableRow = $(this).closest('tr');
    alert('check state ' + $("[class*='box']").is(':checked') ? 1 : 0);    
});

My alert() always returns 1 whether the checkbox is checked or unchecked. Maybe my coding approach could be wrong. Can anyone help to get the right state of checkbox? Thanks

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Thomas
  • 33,544
  • 126
  • 357
  • 626

1 Answers1

3

Use this:

$(document).on('change', '[class*="box"]', function () {
    var tableRow = $(this).closest('tr');
    alert('check state ' + ((this.checked) ? 1 : 0));
});

Also ternary conditions need extra brackets for better functioning.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252