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
alert
function is onwindow
(or the implicitglobal
object). It is not on the returneddiv
element (which is an instance ofHTMLDivElement
).Change your function to this:
Note that this also works (the same
alert
function is being used, becausewindow
is theglobal
object):myFunction:
onload
is not defined onHTMLDivElement
. You should usedocument.addEventListener( 'DOMContentLoaded', handler )
instead.Change your HTML and JavaScript function to this: