How to pass event (e) and variables using event listeners?

I have an event listener:

div.addEventListener('mouseover',function(){bubble_info.call(data);},false); function bubble_info(e,info){ //get e.pageX etc //do stuff with info } 

This problem is in bubble_info , variable e contains information data and info undefined

How can I make sure that I am getting e and info correctly?

+4
source share
2 answers

An event object has many useful properties and methods .

 div.addEventListener('mouseover',function(event){ bubble_info(event, info); // you can pass additional params that can be used in your handler },false); function bubble_info(event, info){ // you can access type of event from event object properties console.log(event.type); console.log(info); // your additional parameter. }; 

addEventListener Documentation

Use the call only if you need to pass a reference to the this object (current). The syntax will be ...

 FunctionName.call(thisArg, arguments-list, ...); 

call documentation

+1
source

Try this (you do not need a call if you do not pass a specific this link that you did not specify at all in the code):

 div.addEventListener( 'mouseover', function(event) { bubble_info( event, dataYouWant ); }, false } 

For reference, .call () should look like this: bubble_info.call( this, event, etc )

0
source

Source: https://habr.com/ru/post/1494423/


All Articles