Fmdpa Posted August 7, 2010 Share Posted August 7, 2010 I'm trying to create a page that has multiple images, and when you roll over the images, a single box will display dynamic captions based on the image being hovered over. With unobstrusive javascript in mind, I tried doing something like this: document.getElementById("img1").onmouseover = function() {caption()};function caption() { document.getElementById("caption").innerHTML = "Caption for image1";} It doesn't work. My logic for doing it this way was that if .innerHTML displays the value of the text enclosed in that tag, then couldn't you set the value of the tag via javascript use .innerHTML? What is the proper way to do this? Link to comment Share on other sites More sharing options...
HungryMind Posted August 7, 2010 Share Posted August 7, 2010 HI!Trydocument.getElementById("myh3").firstChild.data = "Caption for image1";<h3 id="myh3">My Caption</h3> Link to comment Share on other sites More sharing options...
dsonesuk Posted August 7, 2010 Share Posted August 7, 2010 depends where you placed the javascript code? In between the head tags, it will attempt to add the onmouseover function to a element that has not been rendered yet.if it is placed below image and header it should work fine. if within head tags trywindow.onload=function(){document.getElementById("img1").onmouseover = function() {caption()};} Link to comment Share on other sites More sharing options...
Fmdpa Posted August 7, 2010 Author Share Posted August 7, 2010 Yes, it had to do with the placing of the script. Moving it below the body worked (with .innerHTML = "x") and I assume window.onload would work too, though I never tried it. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.