Johan
Johan

Reputation: 41

Change div background color if checkbox inside is checked

I'm looking for a way to change the div background color which is the parent of a checkbox. However I only want it to change background color when the attribute 'checked' is inside the tag.

HTML

<div class="company">
        <input type="checkbox" name="Selected[]" class="checkboxC" value="8" checked>
             Company 8
</div>
<div class="company">
        <input type="checkbox" name="Selected[]" class="checkboxC" value="9">
             Company 9
</div>

So in the example above, I'd only want the div with checkbox 8 to change background color. 9 Stays the default color. I also can't do anything with the name attribute because that's taken by PHP. Any help on how I can fix this using Javascript/jQuery?

Upvotes: 0

Views: 2345

Answers (1)

dreyescat
dreyescat

Reputation: 13798

You could use jQuery to update the parent background color of all those checked inputs once the document is ready:

$(function () {
    $('input:checked').parent().css('background-color', 'red');
});

See a demo

Upvotes: 1

Related Questions