This seems really trivial question but I cannot for the life work this one out.
I have this jQuery var…
var $selectArea = $("ul#1 li input[type='checkbox'],
ul#2 li input[type='checkbox'], ul#3 li input[type='checkbox']");
and I want to run this…
$selectArea.closest('li').addClass('box-checked');
but only if the $selectArea attribute checked is "checked" – pretty simple right?
So I’ve tried this…
if ( $selectArea.attr("checked") == "checked" ) {
$selectArea.closest('li').addClass('box-checked');
}
and…
$selectArea.filter('[checked="checked"]').closest('li').addClass('box-checked');
and this… (but think im sniffing up the wrong tree here)
$selectArea.is('[checked="checked"]').closest('li').addClass('box-checked');
And ideas would be awesome thanks.
It’s because you’re doing it inside the
changeevent for the checkbox. Go ahead an check one of the checkboxes – it will have the class added.I would make the code that you want to happen to checked checkboxes a separate function so you can easily call it
onloadandonchangeand have the same results.Heres an updated fiddle showing you what I mean.