Both onclick and addEventListener javascript function will work but sometimes it creates problem for developers to understand what the exact difference of these two's are.
var h = document.getElementById('a');
h.onclick = function1;
h.onclick = function2;
h.addEventListener('click', function3);
h.addEventListener('click', function4);
In above code, functions 2, 3 and 4 work, but 1 does not. The reason behind that is that the javascript function "addEventListener" does not overwrite existing event handlers, but javascript function "onclick" overrides any existing onclick = fn event handlers.
Also addEventListener does not work in Internet Explorer before version 9. whereas onclick function will work in every browser.
0 Comment(s)