Event listener does not work with pre-defined function as parameter

user74843

I'm a beginner and I have just tried to do this:

ul.addEventListener("click", function(e) {
  console.log("Hi");
});

This works. I understand that this is an anonymous function. However, when I try to give a definition of this function beforehand and pass it in like so, it won't work:

function myFunc(e) {
  console.log("Hi from myFunc");
}

ul.addEventListener("click", myFunc(e));

I do not understand what the difference is. The error reads:

Uncaught ReferenceError: e is not defined
at javascript.js:29

Suren Srapyan

Pass the reference of the function, not the result of it's execution. It will call your function when click event will be fired. It is the same as you pass through an anonymous function, in 2 cases you pass only the reference of the function.

function myFunc(e) {
  console.log("Hi from myFunc");
}

ul.addEventListener("click", myFunc);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Event listener doesn't work. it says event is not defined in the console

From Dev

JS Event Listener with a function that has one parameter

From Dev

Google Map Event Listener does not seem to work

From Dev

Google Map Event Listener does not seem to work

From Dev

event listener does not work after append

From Dev

passing parameter for event listener

From Dev

Javascript custom function with a callback parameter to be executed in a event listener with different parameters?

From Dev

How does the event parameter work in javascript?

From Dev

How to get the parameter to work inside a event function

From Dev

How to get the parameter to work inside a event function

From Dev

Event listener defined inside object

From Dev

Event listener defined inside object

From Dev

Where does execution of code continue after a function is called by an event listener?

From Dev

why does the event listener in this throttling function behave like that?

From Dev

why does the event listener in this throttling function behave like that?

From Dev

Event listener callback does not fire via a normal function call

From Dev

Why does mousedown event listener run through function?

From Dev

"Close" function event listener

From Dev

Pass a parameter to f:event listener

From Dev

jQuery - binding event to named function does not work?

From Dev

Jquery Functions does not work on function parameter

From Dev

Why my implicit function parameter does not work?

From Dev

Javascript "err" function parameter - how does it work?

From Dev

Javascript "err" function parameter - how does it work?

From Dev

How does the parameter 'a' of the numpy mean function work?

From Dev

Removing popups defined in other event listener functions

From Dev

How to modify forloop function to work with SQL queries instead of pre defined values

From Dev

How is function parameter defined?

From Dev

Function is not defined, parameter formatting

Related Related

  1. 1

    Event listener doesn't work. it says event is not defined in the console

  2. 2

    JS Event Listener with a function that has one parameter

  3. 3

    Google Map Event Listener does not seem to work

  4. 4

    Google Map Event Listener does not seem to work

  5. 5

    event listener does not work after append

  6. 6

    passing parameter for event listener

  7. 7

    Javascript custom function with a callback parameter to be executed in a event listener with different parameters?

  8. 8

    How does the event parameter work in javascript?

  9. 9

    How to get the parameter to work inside a event function

  10. 10

    How to get the parameter to work inside a event function

  11. 11

    Event listener defined inside object

  12. 12

    Event listener defined inside object

  13. 13

    Where does execution of code continue after a function is called by an event listener?

  14. 14

    why does the event listener in this throttling function behave like that?

  15. 15

    why does the event listener in this throttling function behave like that?

  16. 16

    Event listener callback does not fire via a normal function call

  17. 17

    Why does mousedown event listener run through function?

  18. 18

    "Close" function event listener

  19. 19

    Pass a parameter to f:event listener

  20. 20

    jQuery - binding event to named function does not work?

  21. 21

    Jquery Functions does not work on function parameter

  22. 22

    Why my implicit function parameter does not work?

  23. 23

    Javascript "err" function parameter - how does it work?

  24. 24

    Javascript "err" function parameter - how does it work?

  25. 25

    How does the parameter 'a' of the numpy mean function work?

  26. 26

    Removing popups defined in other event listener functions

  27. 27

    How to modify forloop function to work with SQL queries instead of pre defined values

  28. 28

    How is function parameter defined?

  29. 29

    Function is not defined, parameter formatting

HotTag

Archive