a1204773
a1204773

Reputation: 7043

How to find who is li's parent?

Let's say we have this list:

<ul title="Fruit">
<li onClick="func(this)">Apple</li>
<li onClick="func(this)">Banana</li>
</ul>
<ul title="Meat">
<li onClick="func(this)">Chiken</li>
<li onClick="func(this)">Duck</li>
</ul>

Is it possible to find from which ul the li is clicked?

Upvotes: 0

Views: 116

Answers (4)

Koerr
Koerr

Reputation: 15723

obj.parentNode or using jQuery just: $(obj).parent()

Upvotes: 0

Jashwant
Jashwant

Reputation: 29005

if(this.parentNode.title === "Fruit") {
 // first one
}
else {
   // the other ul
}

Upvotes: 1

xdazz
xdazz

Reputation: 160833

You should just pass this to the function, and use .parentNode to get its parent.

function func(element) {
  var parent = element.parentNode;
  // ...
}

Upvotes: 1

Jo&#227;o Silva
Jo&#227;o Silva

Reputation: 91299

Yes, use parentNode:

<script type="text/javascript">
    function func(el) {
      alert(el.parentNode.title);    
    }
</script>

<ul title="Fruit">
<li onClick="func(this)">Apple</li>
<li onClick="func(this)">Banana</li>
</ul>
<ul title="Meat">
<li onClick="func(this)">Chiken</li>
<li onClick="func(this)">Duck</li>
</ul>​​​​​

Also note that value is only used for form elements such as input and select.

DEMO.

Upvotes: 1

Related Questions