onClick Function “this” Returns Window Object

That’s because you aren’t passing a reference to this in the JavaScript function call. this in the JavaScript function doesn’t refer to the same object as in the onClick example. Try this instead:

 <li onClick="foo(this)"></li>

 function foo(item){ alert(item.tagName); }

Leave a Comment