Get ID of parent element on click

Javascript is case sensitive for most everything:

<li id='myLi'>A</li>
          ^^--- big L, small i

var x = document.getElementById("myLI").parentNode.nodeName;
                                   ^^---big L, big I

Since you're using an undefined ID, getElementById will return null for "no match found". Null has no "parentNode", hence your error.


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>