Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

addEventListener: How to access event

I have two questions for the following example:

function doIt(){
  this.attribute = someValue; // Works as expected
  alert(event.which); // Doesn't work
}
element.addEventListener("click",doIt,false);

Question 1: Why is this bound to the function but event is not?
Question 2: What would be the appropriate way to do this?

like image 601
Gary Avatar asked Oct 31 '25 01:10

Gary


1 Answers

this is a built-in for JavaScript. It is always accessible. event is not. It is only available if the current method supports it.

You would need to have something like

function doIt(event)

What is this? - http://howtonode.org/what-is-this

like image 79
mrtsherman Avatar answered Nov 02 '25 14:11

mrtsherman