The solution to this problem is probably pretty simple, but I need some help.
var x;
for(x in document.getElementsByTagName("img"))
x.addEventListener('click',openPage, false);
function openPage() {
alert("clicked");
}
I’m not getting an alert when I click on an <img src="something" /> tag. Anyone know why? Also, is my loop necessary?
This code produces an error – in a for..in statement, ‘x’ is the key of your object (in this case, your
document.getElementsByTagNamecall). What you want is:Might I suggest using a Javascript framework (like jQuery), which can help simplify your code as such: