Wednesday, 11 September 2013

OnClick or OnHover Event Listen For All IMG in javascript

OnClick or OnHover Event Listen For All IMG in javascript

I have a list of images I would like to listen for click actions or hover
actions on any "img" on my page regarless of ID. And I want to use regular
javascript and be friendly with modern browsers.
I have done a lot of research and reading but I have yet to find a
solution. I am thinking that my code might look something like this but
I'm not positive:
var allimg = document.getElementsByTagName('img');
for(var i = 0; i < allimg.length; ++i) {
// do something
}
How do I add onlick to this, do I need to call it as a function on
pageload? how do I have the javascript react to all img tags when clicked?

No comments:

Post a Comment