Actually what your jquery block does is listening for any click within the document, and when there is a click it checks if the element that was clicked is <myElement>
.
You can do the same with a code that is similar to this:
document.addEventListener('click', function(event) {
if(event.target.nodeName == 'myElement'.toUpperCase()) {
// do something to myElement
}
});
Update
To have the answer up-to-date with the change of the question, I added an example to demonstrate the differences between mouseover
and mouseenter
:
Works much better with snippet in full-screen mode
document.addEventListener('mouseover', function(event) {
console.log('mouseover '+ event.target.id);
if(event.target.nodeName == 'myElement'.toUpperCase()) {
// do something to myElement
}
});
document.addEventListener('mouseenter', function(event) {
console.log('mouseenter '+ event.target.id);
if(event.target.nodeName == 'myElement'.toUpperCase()) {
// do something to myElement
}
});
html, body {
margin: 0;
padding: 0;
}
div {
margin: 15px;
padding: 15px;
border: 1px dotted blue;
}
body > div {
margin: 0;
}
div div {
border-color: red;
}
div div div {
border-color: green;
}
<div id="outer1"> Outer 1
<div id="outer2"> Outer 2
<div id="inner"> Inner </div>
</div>
</div>
Note that mouseenter
event will fire only once (for the document
object) while the mouseover
event will fire for every change of element within the document
(this will happen for every element in the dom tree).
As for jQuery
The way jQuery overcome this behavior to make this
$(document).on('mouseenter', 'myElement',
work, is that they change the mouseenter
event to mouseover
(on the document
) and they do internal checking to see if the event was a mouseover
or mouseenter
on myElement
.