I'm trying to display ID of parent UL
on LI
click. but the alert is displaying value as undefined. Where am I going wrong?
HTML:
<ul id='uiID'>
<li id='myLi'>A</li>
</ul>
JavaScript
var x = document.getElementById("myLI").parentNode.nodeName;
alert(x.id);
You're not attaching any click event to the element and nodeName
in your case returns LI
, so that's not wanted. What you want is
document.getElementById("myLI").onclick = function(e){
alert(e.target.parentNode.id);
}
You can do something like this :
<ul id='uiID'>
<li id='myLi' onclick="alert(this.parentNode.id)">A</li>
</ul>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With