This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

setAttribute for img event handler

 
Richard Robbins
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic