Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checkbox manipulation

Tags:

jquery

For some reason this little code is preventing the user from being able to check the actual checkbox and having it put the checkmark inside of it and the only way to get it checked is to click the row.

$('table tr').click(function() {

    checkBox = $(this).children('td').children('input[type=checkbox]');

    if(checkBox.attr('checked'))
        checkBox.removeAttr('checked');
    else
        checkBox.attr('checked', 'checked');

});
like image 343
Jeff Davidson Avatar asked Jun 21 '26 20:06

Jeff Davidson


1 Answers

The reason you are disabling the default checkbox behavior of the input is that the checkbox is inside the tr, so when you check the checkbox you fire your Javascript AND you toggle the checkbox... leading to nothing happening. You must check that the target of your event is not a :checkbox.

Also, there's no need to worry about the attribute of the checkbox... with jQuery, you can simply .click() it!

$('table tr').click(function(event) {      

    if (! $(event.target).is("input:checkbox"))
        $(this).find('input:checkbox').click();        
});

Working example

like image 110
Peter Ajtai Avatar answered Jun 24 '26 09:06

Peter Ajtai



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!