Brian Webster
Brian Webster

Reputation: 30855

Javascript or Jquery - on Checkbox Click, how do I change the parent-element's background color?

onclick, I need to

  1. Locate the parent element
  2. Change its background color to red

I'm having particular difficulty with the first part.

Upvotes: 0

Views: 570

Answers (2)

mkilmanas
mkilmanas

Reputation: 3485

$('.grid input[type="checkbox"]').click(function(){
    $(this).parent().css('background-color', 'red');
});

UPDATE (for attribute use)

function clickHandler() {
    $(this).parent().css('background-color', 'red');
}

and then onclick='clickHandler()'

Upvotes: 3

Nicola Peluchetti
Nicola Peluchetti

Reputation: 76880

if you attach the event with jquery it's pretty easy

$('#yourid').click(function(){
     var parent = $(this).parent();
     parent.css("background-color", "red");
});

Upvotes: 1

Related Questions