JS li tag onclick not working on IE8

IE8 and earlier don’t have addEventListener, but they do have its non-standard predecessor, attachEvent. They’re not quite the same. Here’s a “hook this event” function that uses what’s available: var hookEvent = (function() { var div; // The function we use on standard-compliant browsers function standardHookEvent(element, eventName, handler) { element.addEventListener(eventName, handler, false); return element; } … Read more

addEventListener calls the function without me even asking it to

Quoting Ian’s answer: Since the second parameter expects a function reference, you need to provide one. With your problematic code, you’re immediately calling the function and passing its result (which is undefined…because all the function does is alert and doesn’t return anything). Either call the function in an anonymous function (like your first example) or … Read more

addEventListener vs onclick

Both are correct, but none of them are “best” per se, and there may be a reason the developer chose to use both approaches. Event Listeners (addEventListener and IE’s attachEvent) Earlier versions of Internet Explorer implement javascript differently from pretty much every other browser. With versions less than 9, you use the attachEvent[doc] method, like … Read more