How to pass arguments to addEventListener listener function?

前端 未结 30 2295
谎友^
谎友^ 2020-11-21 23:56

The situation is somewhat like-

var someVar = some_other_function();
someObj.addEventListener(\"click\", function(){
    some_function(someVar);
}, false);
<         


        
相关标签:
30条回答
  • 2020-11-22 00:31

    someVar value should be accessible only in some_function() context, not from listener's. If you like to have it within listener, you must do something like:

    someObj.addEventListener("click",
                             function(){
                                 var newVar = someVar;
                                 some_function(someVar);
                             },
                             false);
    

    and use newVar instead.

    The other way is to return someVar value from some_function() for using it further in listener (as a new local var):

    var someVar = some_function(someVar);
    
    0 讨论(0)
  • 2020-11-22 00:31

    The following answer is correct but the below code is not working in IE8 if suppose you compressed the js file using yuicompressor. (In fact,still most of the US peoples using IE8)

    var someVar; 
    someVar = some_other_function();
    alert(someVar);
    someObj.addEventListener("click",
                             function(){
                              some_function(someVar);
                             },
                             false);
    

    So, we can fix the above issue as follows and it works fine in all browsers

    var someVar, eventListnerFunc;
    someVar = some_other_function();
    eventListnerFunc = some_function(someVar);
    someObj.addEventListener("click", eventListnerFunc, false);
    

    Hope, it would be useful for some one who is compressing the js file in production environment.

    Good Luck!!

    0 讨论(0)
  • 2020-11-22 00:32

    There is absolutely nothing wrong with the code you've written. Both some_function and someVar should be accessible, in case they were available in the context where anonymous

    function() { some_function(someVar); } 
    

    was created.

    Check if the alert gives you the value you've been looking for, be sure it will be accessible in the scope of anonymous function (unless you have more code that operates on the same someVar variable next to the call to addEventListener)

    var someVar; 
    someVar = some_other_function();
    alert(someVar);
    someObj.addEventListener("click", function(){
        some_function(someVar);
    }, false);
    
    0 讨论(0)
  • 2020-11-22 00:32

    You can add and remove eventlisteners with arguments by declaring a function as a variable.

    myaudio.addEventListener('ended',funcName=function(){newSrc(myaudio)},false);

    newSrc is the method with myaudio as parameter funcName is the function name variable

    You can remove the listener with myaudio.removeEventListener('ended',func,false);

    0 讨论(0)
  • 2020-11-22 00:32

    In 2019, lots of api changes, the best answer no longer works, without fix bug.

    share some working code.

    Inspired by all above answer.

     button_element = document.getElementById('your-button')
    
     button_element.setAttribute('your-parameter-name',your-parameter-value);
    
     button_element.addEventListener('click', your_function);
    
    
     function your_function(event)
       {
          //when click print the parameter value 
          console.log(event.currentTarget.attributes.your-parameter-name.value;)
       }
    
    0 讨论(0)
  • 2020-11-22 00:35

    Quite and old question but I had the same issue today. Cleanest solution I found is to use the concept of currying.

    The code for that:

    someObj.addEventListener('click', some_function(someVar));
    
    var some_function = function(someVar) {
        return function curried_func(e) {
            // do something here
        }
    }
    

    By naming the curried function it allows you to call Object.removeEventListener to unregister the eventListener at a later execution time.

    0 讨论(0)
提交回复
热议问题