I am trying something simple in jQuery to help me get a handle on a bigger issue - I have a series of checkboxes, each inside a div
When the page loads, if the checkbox is checked I want the closest div to change colour BUT only the closest one and not the others
<form>
<div class="col-lg-6">
<input type="checkbox" class="go" checked="checked"/>
</div>
<div class="col-lg-6">
<input type="checkbox" class="go"/>
</div>
</form>
jQuery:
if ($('.go').is(':checked')) {
$(this).closest('.col-lg-6').css('background-color','red');
}
I cannot change the selectors etc nor the HTML layout - why doesn't this recognise the instanced of go and change the closest div?
In the above it doesn't do anything - if I change the jQuery to:
if ($('.go').is(':checked')) {
$('.go').closest('.col-lg-6').css('background-color','red');
}
then it colours both div's which I don't want