I have a webpage with many images, each of which has a title
attribute. When I hover over the image, I want the title
text to display in a separate legend
element I have created (not as a tiny hover tooltip). I have a mouseover
function which works fine - i.e. when I hover over the image the legend changes value. But I want that value to be the title
text of the individual image - which I don't know how to access. I have tried …innerHTML = this.title
which is obviously incorrect.
[The number of images is large and changing (like an album), so I can't add separate code for each <img>
tag. I can use alt
or any other <img>
attribute to make this work. I'm not wedded to the innerHTML
method, either, but am hoping for some simple code to do the trick!]
Please help? Thanks! (FYI, I'm a novice coder.)
<!DOCTYPE html>
<html>
<body>
<h1 id="legend">title appears here</h1>
<div>
<img src="image1.jpg" onmouseover="mouseOver()" title="Breakfast">
<img src="image2.jpg" onmouseover="mouseOver()" title="Lunch">
<img src="image3.jpg" onmouseover="mouseOver()" title="Dinner">
</div>
<script>
function mouseOver() {
document.getElementById("legend").innerHTML = this.title;
}
</script>
</body>
</html>