wood burning stoves*
The moose likes HTML, CSS and JavaScript and the fly likes setAttribute for img event handler Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "setAttribute for img event handler" Watch "setAttribute for img event handler" New topic
Author

setAttribute for img event handler

Richard Robbins
Ranch Hand

Joined: Mar 07, 2001
Posts: 66
Hello,
I am dynamically creating an image in an HTML document using "nextImage = document.createElement( "img" )", and I would like to be able to add event handlers that apply to this image (onclick, onmouseover, onmouseout). Whenever I add the code: "nextImage.setAttribute( "onmouseover", "swapImage()" )", nothing happens when I mouse over the image. Is is possible to add an event handler to a dynamically created element using setAttribute or any other DOM method?
As a note, I tried using an anchor as the image's parent, and that allowed me to use setAttribute( "href", "javascript:...") as a substitute for onclick, but I don't want the image to be the child of an anchor, and I still have the problem of rotating images for onmouseover and onmouseout events. Is there an expert out there who is more familiar with this than I am and can help out?


Richard Robbins is one of the founders of WebsiteTemplateDatabase.com, a resource for free website templates and Photoshop tutorials. He also consults for an online boutique selling baptism dresses and christening gowns.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: setAttribute for img event handler
 
Similar Threads
images and event handlers
onclick
On mouse over / On click events
how to set onClick event of anchor using javascript?
javascript