Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery Toggle Checkbox

Tags:

jquery

So I have an issue. I'm trying to toggle checkboxes in order to add some css to the parent <tr>

This is what I have so far

 $('input[type=checkbox]').toggle(function(){

    $(this).parents('tr').addClass('selectCheckBox', 970);
},
function(){

    $(this).parents('tr').removeClass('selectCheckBox', 970);
});

However, the result of this is that the <tr> does receieve the selectCheckBox but the checkbox does not become checked. So I tried the following:

 $('input[type=checkbox]').toggle(function(){
    $(this).attr('checked',true);
    $(this).parents('tr').addClass('selectCheckBox', 970);
},
function(){
    $(this).attr('checked',false);
    $(this).parents('tr').removeClass('selectCheckBox', 970);
});

Once again, no luck.

I believe it has to do with how the checkbox id looks.

<input type="checkbox" name="select[]" id="select[]">

I then tried escaping the [] by using \\[\\] as I've used in the past, but no luck.

Is there a way to make the checkbox checked?

like image 444
Dimitri Avatar asked Nov 03 '22 08:11

Dimitri


1 Answers

Try on change event. Also as I think toggle event is deprecated and addClass doesn't take a second parameter (unless you're using jQuery UI).

$('input[type=checkbox]').change(function() {
  $(this).parents('tr').toggleClass('selectCheckBox', $(this).is(':checked'));
});
like image 70
elclanrs Avatar answered Nov 15 '22 06:11

elclanrs