posted 22 years ago
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?