I am encountering an issue with hiding an image using the onmouseover event not applied directly to it, but rather to a button element. The desired functionality is for the image to appear when the mouse hovers over and disappear when it moves away. Here's my current code:
<script>
function toggleImage(id) {
var x = document.getElementById(id);
if (x.style.display === 'none') {
x.style.display = 'block';
} else {
x.style.display = 'none';
}
}
</script>
This JavaScript function allows for dynamic toggling of images on mouseover, and in the HTML section you can find:
<!-- Example image elements -->
<img id="imgSWTCH1" src="https://www.shareicon.net/data/128x128/2016/10/20/846459_blue_512x512.png" width="100" height="100"/>
<img id="imgSWTCH2" src="https://www.writeraccess.com/wp-content/uploads/2014/08/blog-html-5.png" width="100" height="100"/>
<!-- Add more image elements as needed -->
<!-- Using buttons to trigger image toggling -->
<button onmouseover="toggleImage('imgSWTCH1')" onmouseout="toggleImage('imgSWTCH1')">Toggle Image 1</button>
<button onmouseover="toggleImage('imgSWTCH2')" onmouseout="toggleImage('imgSWTCH2')">Toggle Image 2</button>
<!-- Add more buttons to correspond with additional images -->
The `onmouseout` event currently changes the background color to orange, but the goal is to hide the corresponding image instead. However, assigning multiple IDs to an element poses a challenge. A potential solution would be implementing this functionality using jQuery or Angular.
Here is the Plunker link, make sure to expand the HTML section to view the complete code.