CodeOverload
CodeOverload

Reputation: 48485

Changing DIV color without giving an ID?

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

Upvotes: 0

Views: 363

Answers (2)

Sampson
Sampson

Reputation: 268364

$("a").click(function(e){
  e.preventDefault();
  $(this).parent().parent().css("color", "green");
});

Upvotes: 7

nickf
nickf

Reputation: 546085

Try this:

$('a').click(function() {
    $(this).parent().parent().css('backgroundColor', 'yellow');
    return false;
});

Upvotes: 4

Related Questions