I am trying to execute code when the onmuseout/onload events occur, but for some reason my code is not running??
function outFunction() {
document.getElementById("mouseout").alert("Don't Leave!")
}
function myFunction() {
document.getElementById("hi").innerHTML = "This DIV is loaded."
}
<div onmouseout="outFunction()" , id="mouseout">DIV</div>
<div id="hi" onload="myFunction()"></div>
outFunction:
The
alertfunction is onwindow(or the implicitglobalobject). It is not on the returneddivelement (which is an instance ofHTMLDivElement).Change your function to this:
Note that this also works (the same
alertfunction is being used, becausewindowis theglobalobject):myFunction:
onloadis not defined onHTMLDivElement. You should usedocument.addEventListener( 'DOMContentLoaded', handler )instead.Change your HTML and JavaScript function to this: