Elankeeran
Elankeeran

Reputation: 6184

Onclick on image I need to change the background of li using jquery

Onclick on image I need to change the background of li using jquery

LI is not immediate parent so I am not able to use parent() function in jquery

<li class="ui-widget-content ui-corner-tr ui-draggable">  
    <fb:profile-pic height="32" width="32" linked="false" uid="557103888" style="width: 32px; height: 32px;" class=" fb_profile_pic_rendered">  
        <img class="" style="width: 32px; height: 32px;" title="some name" alt="some name" src="files/t557103228_5135.jpg">  
    </fb:profile-pic> 
</li>

Upvotes: 1

Views: 1152

Answers (3)

Sarfraz
Sarfraz

Reputation: 382696

You can do like this:

$('img[title="some name"]').click(function(){
  $(this).closest('li.ui-widget-content').css('background', 'green');
});

Since the image does not have any class or id attribute set, the title attribute is used instead. In this case any image whose title is set to some title for example gets clicked, the background of parent li with class ui-widget-content is changed using css method which is actually found with closest function which finds elements back up until specified element is found.

Upvotes: 1

user113716
user113716

Reputation: 322492

Use .closest('li').

This will give you the first ancestor of the <img> that is an <li> element.

You could also use .parents('li:first'). It uses the :first selector to ensure you only get the first result.

Upvotes: 3

kennytm
kennytm

Reputation: 523264

It's the grandparent, so .parent().parent() then.

Or use .closest('li').

Upvotes: 0

Related Questions