There is an example in "Head first JavaScript" book. This piece of code is unblurring an image on click. The code works, but I don't understand how, though it's an extremely simple piece of code.
The function init
is called when a window is loaded. getElementsByTagName
gives an HTMLCollection. A click on an image invokes a showAnswer
function.
Now there is a mystery for me.
window.onload = init;
function init() {
var images = document.getElementsByTagName("img");
for (var i = 0; i < images.length; i++) {
images[i].onclick = showAnswer;
}
};
function showAnswer(e) {
var image = e.target;
var name = image.id;
name = name + ".jpg";
image.src = name;
}
- There should be a parameter
e
. How this parameter is being created, from where? WhenshowAnswer
is called ininit
, there are no parameters given to it. - Considering the fact that I'm using a
.target
method on it, it should be an object. How does the browser know that this object has namee
? - Why
images[i].onclick = showAnswer;
and notshowAnswer();
?