0

My question is simple but little bit confusing for me that when we write code of document.onclick with function function(e) so why we put e into parameter even though function is not return anything in call back function's paramter because it's anonymous function so my point is that why we don't put e or event inside the function instead of parameter().

var divs = document.getElementsByTagName('div');
for(var i =0; i<divs.length; i++){
    divs[i].onclick = function(e){
        e = e || event;
        var target = e.target || e.srcElement;
        this.style.backgroundColor='yellow'
        e.stopPropagation();
        alert("Target " + target.className + ",this " + this.className);
    }
}
Nope
  • 22,147
  • 7
  • 47
  • 72
Haseeb Jumani
  • 65
  • 1
  • 6
  • 'e' stands for 'Event'. This is on abject representing the event that was triggered and contains a lot of useful informations, like mouse cursor position when you clicked a button, or event modifiers keys such as Shift, Ctrl of Alt when you're inputing some text on an element with an eventListener. – Sir McPotato Jan 23 '17 at 09:54
  • More specifically, ``e`` is the data that is passed to the callback that is *called back* when the event occurs. ``e`` could be called ``Cake`` for all it matters, it would still get passed that data and contain event-related properties. – Joshua Jan 23 '17 at 10:13
  • so why we don't put 'e' inside the function instead of parameter? – Haseeb Jumani Jan 23 '17 at 12:45

0 Answers0