I am new to Javascript and trying to understand using event.target
to reference the element that fired the event. All the documentation says I should pass event
as a parameter to any function that requires it, but I have found that event.target
works even if I don't pass the parameter.
e.g. all the following work and give the same result:
<button type="button" onclick="f1(event);">Calls function f1 passing event</button>
<button type="button" onclick="f1();">Calls function f1 without passing event</button>
<button type="button" onclick="f2(event);">Calls function f2 passing event</button>
<script>
function f1() {
alert(event.target.innerHTML);
}
function f2(event) {
alert(event.target.innerHTML);
}
</script>
So my question is, what is the point passing event
? Is it just considered good practice to send it? Or will some browsers not work if I don't?
I think it looks neater not to have to pass it, so if I can get away without doing so, I'd rather, but I don't want to fly in the face of good practice.
Thanks in advance.