I have a JS function invoked by an onmouseenter event, whoose aim is to write something in the innerHtml of the invoking object, when I go in and out of the object really fast with the mouse, my function fails:
Uncaught TypeError: Cannot read property 'id' of null
at showTextOnav (main.js:99)
at HTMLElement.onmouseenter (index.html:1)
showTextOnav @ main.js:99
onmouseenter @ index.html:1
I investigated and found out that, the parent of the object, passed to the function by the event, has no id, while it should have an id. is it possible that the onmousenter event is not passing the object at all?
js code:
function showTextOnav(obj, description){
console.log(obj.parentNode.id);
content = document.getElementById(obj.parentNode.id).innerHTML;
var htmlDescription = '<div onmouseleave="hideTextOnav(this, \''
+ description + '\')">' + description + '</div>';
console.log(content);
if(!(content.includes(htmlDescription))){
console.log("showTextOnav");
console.log(obj.parentNode.id);
document.getElementById(obj.parentNode.id).innerHTML += (htmlDescription);
}
}
function hideTextOnav(obj, description){
let content = document.getElementById(obj.parentNode.id).innerHTML;
var htmlDescription = '<div onmouseleave="hideTextOnav(this, \''
+ description + '\')">' + description + '</div>';
console.log(content);
if((content.includes(htmlDescription))){
console.log("hideTextOnav");
console.log(obj.parentNode.id);
console.log(content);
console.log(htmlDescription);
content = content.replace(htmlDescription, '');
document.getElementById(obj.parentNode.id).innerHTML = content;
}
}
HTML code:
<div id="onavbar" class="onavbar">
<a onclick="toggleOnav()" id="blocktoggleonav" class="right" style="display: none";>
<i class="material-icons" id="toggleonav">keyboard_arrow_right</i>
</a>
<a onclick="toggleVnav()" id="blocktogglevnav">
<i class="material-icons" id="togglevnav" onmouseenter="showTextOnav(this, 'SIDEBAR')"
onmouseleave="hideTextOnav(this, 'SIDEBAR')">dehaze</i>
</a>
<a class="active" href="index.html" id = "gotoindex">
<i class="material-icons" onmouseenter="showTextOnav(this, 'HOME')"
onmouseleave="hideTextOnav(this, 'HOME')">home</i>
</a>
<a href="chisono.html" id = "gotowhoami">
<i class="material-icons" onmouseenter="showTextOnav(this, 'INFO')"
onmouseleave="hideTextOnav(this, 'INFO')">person</i>
</a>
<a href="novità.html" id = "gotonews">
<i class="material-icons" onmouseenter="showTextOnav(this, 'NEWS')"
onmouseleave="hideTextOnav(this, 'NEWS')">new_releases</i>
</a>
<a href="esperimenti.html" id = "gotoexperiments">
<i class="material-icons" onmouseenter="showTextOnav(this, 'TRY ME')"
onmouseleave="hideTextOnav(this, 'TRY ME')">apps</i>
</a>
</div>