Trigger onmouseover event programmatically in JavaScript

fearofawhackplanet picture fearofawhackplanet · Feb 9, 2010 · Viewed 77.4k times · Source

Is there a way to programmatically trigger the onmouseover event in plain JavaScript? or "extract" the method from the onmouseover event to call it directly?

eg

<div id="bottom-div" onmouseover="myFunction('some param specific to bottom-div');">
    <div id="top-div" onmouseover="????????"></div>
</div>

top-div is above bottom-div, so the onmouseover won't get fired in bottom-div. i need a way of calling myFunction('some param specific to bottom-div'); from top-div

Answer

givehug picture givehug · Mar 13, 2018
const mouseoverEvent = new Event('mouseover');

whateverElement.dispatchEvent(mouseoverEvent);