Gaurav
Gaurav

Reputation: 8487

jquery getting listitem id from its child id

<li id="l"><a href="#"><img id="i" /></a></li>

I have image id and i want listitem id , i know one way

$("#i").parent().parent().attr("id");

Is there any better way than this?

Upvotes: 0

Views: 76

Answers (3)

KoolKabin
KoolKabin

Reputation: 17653

Try closest or find .

$("#i").closest('li').attr("id")

Upvotes: 0

adeneo
adeneo

Reputation: 318182

$("#i").closest('li')[0].id;

http://api.jquery.com/closest/

Get the first element that matches the selector, beginning at the current element and progressing up through the DOM tree.

Upvotes: 0

Jonny Burger
Jonny Burger

Reputation: 921

This is a bit simpler:

$("#i").parents("li").attr("id")

Upvotes: 3

Related Questions