onmouseover and hyperlink hover
-
25-09-2019 - |
Question
I had a div with links that underlined once you hover over them. I added an onmouseover
JS event to the div and now the hyperlinks no longer underline when I hover over them, but instead whatever action I put into the onmouseover
event gets executed instead.
CODE from function:
function addPlus(elementId)
{
if(typeof addPlus.backup == 'undefined')
addPlus.backup = document.getElementById(elementId).innerHTML;
if(full)
{
document.getElementById(elementId).innerHTML = plusCode + addPlus.backup;
return backup;
}
else
return snippet;
}
div code:
<div class="nav_bar" id="navbar1" onmouseover="addPlus('navbar1')" onmouseout="removePlus('navbar1')">
EDIT: I've tried return true;
, return Boolean(true);
, and return new Boolean(true);
, in an attempt to "return true" as Chad suggested. None of them work. Sorry, I really don't know what to do; I'm new to javascript.
EDIT 2: Darn it, I just realized Chad meant that I return true in the div tag. So now I have <div class="nav_bar" id="navbar1" onmouseover="addPlus('navbar1');return true" onmouseout="removePlus('navbar1')">
, but it unfortunately still doesn't work.
Solution
If you don't want for js event to fire you should stop event propogation when mouse enters <a>
. Try adding onmouseover event handler for each hyperlink with return false - <a onmouseover="return false" ...
- that should help.
OTHER TIPS
for the event to propagate you need to return true from your onmouseover handler.
- This won't occur if you use CSS to underline your links instead of JavaScript
- You presumably have some rotten code that is explicitly setting an
onmouseover
handler in HTML or JavaScript to handle the link underline (instead of properly usingaddEventListener
), and then you are overwriting this handler. It's hard to guess what might be wrong without more information. Supply a code example for more help.