/    Sign up×
Community /Pin to ProfileBookmark

Text hyperlinks triggering false onmouseout event. Please help.

Here’s the situation. I have a one-cell table with some text in it. When you mouse over the cell, the text changes. When you mouse out of the cell, the text changes to something else. Simple stuff.

The problem I’m having is that once you mouse into the cell, the new text to be shown is a hyperlink. Apparently, if you’re inside the cell but over a hyperlink, “onmouseout()” gets called because the browser thinks your mouse has left the table. But really, it’s just hovering on top of a hyperlink inside the table. Follow?

The result of this issue is that you see text flickering back and forth between two different sentences and you can’t read the new text let alone click the hyperlink. I’m sure this has been solved more than once in the past but I haven’t been able to conjure up the right search phrase to describe this problem. Please help…

[CODE]
<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″>
<title>Test</title>

<script type=”text/javascript” language=”javascript”>
function changeText()
{
document.getElementById(“myText”).innerHTML = ‘<a href=”link.html”>A hidden link! Click me!</a>’;
}
function restoreText()
{
document.getElementById(“myText”).innerHTML = “Shh! It’s a secret!”;
}
</script>

</head>
<body>

<table bgcolor=”red” width=”300″ align=”center”>
<tr>
<td align=”center” onmouseover=”changeText()” onmouseout=”restoreText()”>
<span id=”myText”>Mouse over me!</span>
</td>
</tr>
</table>

</body>
</html>[/CODE]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@MrNobodyJan 14.2009 — The problem I'm having is that once you mouse into the cell, the new text to be shown is a hyperlink. Apparently, if you're inside the cell but over a hyperlink, "onmouseout()" gets called because the browser thinks your mouse has left the table. But really, it's just hovering on top of a hyperlink inside the table. Follow?[/QUOTE]
Yes, any time a parent element has a child element, mousing over the child element is tantamount to mousing out of the parent element. Text nodes don't count. You actually have two different issues going on here. The second issue is called "event bubbling." When the [B]onmouseover[/B] event for the child element fires, the [B]onmouseover[/B] event for the parent element(s) will also fire all the way back up to the document element. It is easier to handle the event bubbling issue than the other. Canceling event bubbling is pretty standard -- pun intended -- but there are two different event blocks to consider:
[CODE]event.cancelBubble = true;[/CODE]
Try this one first and see what it buys you -- if anything. Do you know how to handle the two different event blocks?
Copy linkTweet thisAlerts:
@conquesimoauthorJan 15.2009 — This is turning out to be much more complicated than I first imagined. I keep trying to find the quick fix to no avail. I'm going to read into Javascript event tutorials now... *sigh*
×

Success!

Help @conquesimo spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...