I am trying to add a class when a checkbox is checked.
My jquery:
$('input').attr("checked").change(function(){
$('div.menuitem').addClass("menuitemshow");
})
You should not use $("input")
to select a checkbox
, input will select all inputs. Instead you can use input:checkbox
:
$('input:checkbox').change(function(){
if($(this).is(":checked")) {
$('div.menuitem').addClass("menuitemshow");
} else {
$('div.menuitem').removeClass("menuitemshow");
}
});
Basically what this does is execute whatever is inside the function(){}
when the checkbox is changed. Then you can just use jQuery is
to check if the checkbox is checked or not..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With