开发者

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

开发者 https://www.devze.com 2023-03-11 02:30 出处:网络
onclick, I need to Locate the parent element Change its background co开发者_JS百科lor to red I\'m having particular difficulty with the first part.$(\'.grid input[type=\"checkbox\"]\').click(funct

onclick, I need to

  1. Locate the parent element
  2. Change its background co开发者_JS百科lor to red

I'm having particular difficulty with the first part.


$('.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()'


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

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

精彩评论

暂无评论...
验证码 换一张
取 消