Is it possible to simulate hover using JavaScript Events? I tried injecting mouseover event on the target element but no luck.
For example, if there is a link that
It is possible to simulate hover using JavaScript events. I created a module for swapping out images on hover. You can experiment and adapt the module to fit your needs. For the example, I made the image paths and DOM selection elements generic.
// module for swapping out images on hover
var imageSwap = (function ModuleFactory() {
"use strict";
var imageContainer = document.getElementById("image-container"),
image = document.getElementsByClassName("image")[0],
imageSource1 = 'path/to/your/image1',
imageSource2 = 'path/to/your/image2';
function handleImageSwap(e) {
if (e.target.id === "image-container") {
image.setAttribute("src", imageSource2);
e.target.addEventListener("mouseleave", function _handler_() {
image.setAttribute("src", imageSource1);
e.target.removeEventListener("mouseleave", _handler_, false);
}, false);
}
}
function init() {
imageContainer.addEventListener("mouseenter", handleImageSwap, false);
}
var publicAPI = {
init: init
};
return publicAPI;
})();
document.addEventListener("DOMContentLoaded", imageSwap.init(), false);