Reputation: 7043
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
Reputation: 29005
if(this.parentNode.title === "Fruit") {
// first one
}
else {
// the other ul
}
Upvotes: 1
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
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