Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Difference between addEventListener and onclick

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 805
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: