Javascript Event Object
In traditional event registration model: function foo(e){console.log(e.type)} document.getElementById('#id').onclick=foo;//registered event handler But in inline event registratio
Solution 1:
Since event object being used within the click function is not passed by the browser we are manually passing it.
That's not correct. The browser (at least W3C compatible browser) pass the event object to the event handler.
The equivalent to
onclick="foo()"
is
elem.onclick = function(event) {
foo();
};
The browser creates a function with event
as first parameter and uses the value of the attribute as body of the function.
You have to pass the event object explicitly to foo
because that's how JavaScript functions work. If you call a function inside another function, the outer function's parameters are not automatically passed to the inner function (that would be really confusing IMO).
Simpler example:
function foo(a) {
bar();
}
function bar(a) {
alert(a); // will show `undefined`
}
foo(42);
Post a Comment for "Javascript Event Object"