addEventListener is not working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • spv2
    New Member
    • Jan 2013
    • 3

    #1

    addEventListener is not working

    Hi,
    This is my code

    Code:
    $("#<%=imgBtDone.ClientID %>").click(function(e) {
    var button = document.getElementById('imgBtDone');
        
    button.addEventListener('click', function () {                       alert('test'); }, false);  
    });
    here addEventListene r not executing.

    Thanks,
    spv2
    Last edited by Rabbit; Jan 19 '13, 06:20 AM. Reason: Please use code tags when posting code.
  • Anas Mosaad
    New Member
    • Jan 2013
    • 185

    #2
    addEventListene r is the standard method for attaching adding events to components. However, IE is not following the standard and has its own functions to attach events (i.e. attachEvent). It's recommended to develop your own functions to workaround cross browser compatibilities . I use this cross browser function to add event listeners:
    Code:
    function attachEventToComponent(component, eventName, eventHandler){
    if (component.addEventListener){
       component.addEventListener(eventName,eventHandler, false);
    }else if (component.attachEvent){
       try{
           component.detachEvent("on"+eventName, eventHandler);
       }catch(e){}
       component.attachEvent("on"+eventName, eventHandler);
    }
    }

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      However, IE is not following the standard and has its own functions to attach events (i.e. attachEvent). It's recommended to develop your own functions to workaround cross browser compatibilities .
      As of IE 9, addEventListene r() is eventually supported.


      I use this cross browser function to add event listeners:
      you are aware that you can’t use this in you event handlers?

      Comment

      • Anas Mosaad
        New Member
        • Jan 2013
        • 185

        #4
        Thanks Dormilich for the clarification. I honestly didn't about it as the function already supports it.
        Regarding this, you can workaround this with closure.

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          Regarding this, you can workaround this with closure.
          workaround for a workaround, eh?

          yea, cross-browser issues are a real bother. not even jQuery tackled the full IE event issue.

          Comment

          Working...