My point is to trigger the onmouseover
event in javascript code. I used the function
onmouseover()
on an element. But it doesn't works in some situations.
This is a demo code:
HTML file:
<p id="p-id">Text Text</p>
External Javascript file:
var myP = document.getElementById("p-id");
myP.addEventListener("mouseover", function() {
this.style.backgroundColor = "red";
});
// trigger the mouseover:
myP.onmouseover(); // console error: onmouseover is not a function.
But onmouseover()
function works well when I use the attribute onmouseover="javascript code"
on the element:
HTML file:
<p id="p-id" onmouseover="this.style.backgroundColor='red'">Text Text</p>
External Javascript file:
var myP = document.getElementById("p-id");
// trigger the mouseover:
myP.onmouseover(); // works well
I have been searching the reason for ages but I have no clues.