views:

90

answers:

2

Hi all.

I have the following code :

<div id="wrapper">
  <div style="width:500px;background-color:yellow;"> // this is a parent div
    <div style="width:260px;">
      <a href="javascript:XXXXX">Click me to color only the FIRST yellow div</a>
    </div>
  </div>

  <div style="width:500px;background-color:yellow;"> // this is a parent div
    <div style="width:260px;">
      <a href="javascript:XXXXX">Click me to color only the SECOND yellow div</a>
    </div>
  </div>
</div>

Can I use jQuery to change the parent div's color without giving its ID or name?

Thanks

+3  A: 

Try this:

$('a').click(function() {
    $(this).parent().parent().css('backgroundColor', 'yellow');
    return false;
});
nickf
Thanks too nick :)
David
+7  A: 
$("a").click(function(e){
  e.preventDefault();
  $(this).parent().parent().css("color", "green");
});
Jonathan Sampson
Thanks it works, but is it possible to append a class to the div? if it can't, how i can add this css : filter:alpha(opacity=40) ? Thanks
David
@unknown: replace `.css("color", "green")` with `.addClass("foo")` to add the class `foo`. You can fade this too by replacing `.css(...)` to `.fadeTo("slow",.4)`.
Jonathan Sampson
Thanks soo much jonathan
David
You're welcome, @unknown. May I encourage you to change your name? You can do so in your profile.
Jonathan Sampson
changed, my name is tom :)
David