What are the ways, and most importantly, how to correctly pass arguments to a function in an event handler
For example:
var menu = document.querySelector('.nav'); var ul = menu.querySelector('ul'); function handler(event, bool) { event = event || window.event; if(event.target.tagName == 'LI') { if(bool) { ul.style.backgroundColor = 'red'; } else { ul.style.backgroundColor = ''; } } } menu.addEventListener('mouseout',handler.bind(null, event, false), false); menu.addEventListener('mouseover',handler.bind(this, event, true), false); It seems to work, but there is no difference what context I pass to bind , at least this , at least null , at least вася .... Next. Initially, the function code was:
function handler(event, bool) { if(event.target.tagName == 'LI') { var ul = menu.querySelector('ul'); if(bool) { ul.style.backgroundColor = 'red'; } else { ul.style.backgroundColor = ''; } } } At the same time event == undefined , ul == undefined .
Added a line to the beginning of the function: event = event || window.event; event = event || window.event; - event appeared, rendered var ul = menu.querySelector('ul'); from function began to work.
So how is it possible to pass arguments?