473,408 Members | 2,399 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

if no "onmouseover" in HTML, how can javascript do an link id - dependent action?

Situation:
Many links with hrefs & some have ids in the HTML.
External javascript file.

Wanted:
When the mouse is hovering over a link with a certain id, do something
(such as display one particular image; which image depends on which
link).
Do this without having onmouseover attributes in the HTML.

Question:
Can I use addEventListener/attachEvent to make the script perceive
WHICH link is hovered over, and feed that link's id to some variable?
If so, HOW?

Note: If I specify "look for this link" (with
document.getElementById('link_id'), the script can listen for the
event. However, when I have more than one specified link_id, only the
last one has any effect. Also, if I write a for loop, so far the same
issue (only the last id has any effect, all the rest are ignored).

Hope this makes sense.

--metasilk
Jul 23 '05 #1
2 1731
On 1 Sep 2004 06:42:31 -0700, metasilk <me******@sover.net> wrote:

[snip]
Can I use addEventListener/attachEvent to make the script perceive WHICH
link is hovered over, and feed that link's id to some variable?
It's possible with addEventListener, but not with attachEvent. I'll
explain in a moment.
If so, HOW?


Just like with intrinsic events specified in HTML, the this operator
refers to the element to which the listener is attached. Once you have a
reference to the element, you can get the id attribute. A demo can be
found at:

<URL:http://www.mlwinter.pwp.blueyonder.co.uk/clj/metasilk/this.html>

So, why won't this work with attachEvent? Because IE doesn't set the this
operator properly. Instead of referencing the element, it used the global
(window) object. To get around this, you can fall back onto the old,
on<event name>, properties if addEventListener isn't available.

Just in case you didn't know, the old event properties don't allow you to
add multiple listeners to a single element without a lot of extra code. On
top of that, any existing listeners specified in the HTML will be
overridden:

<span id="test" onclick="alert('foo');">Test</span>

Clicking the above will display "foo" as expected, but:

<span id="test" onclick="alert('foo');">Test</span>

document.getElementById('test').onclick = function() {
alert('bar');
};

the above result in "bar". If you used addEventListener, you would get
both (foo, first).

That "extra code" is used by the demo above.

[snip]

Hope that helps,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2
metasilk wrote:
...
Wanted:
When the mouse is hovering over a link with a certain id, do something
(such as display one particular image; which image depends on which
link).
Do this without having onmouseover attributes in the HTML.
...

Some related demos:

http://cross-browser.com/toys/link_interception.html
http://cross-browser.com/toys/tristateimage.html
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: mike | last post by:
regards: I use Jtidy (api) to translate a HTML file into a "XHTML file". But The "XHTML file" cannot be identified by nokia 6600. Do I miss something important? Or this is Jtidy's weakness or...
25
by: delerious | last post by:
I see some code examples like this: <DIV onmouseover="this.style.background='blue'"> and other code examples like this: <DIV onmouseover="javascript:this.style.background='blue'"> Which...
3
by: davidkarlsson74 | last post by:
Error: document.getElementById("folderMenu").cells has no properties File: http://www.volkswagen.se/tillbehor/js/foldermenu.js Rad: 49 The function activates different DIV:s, but doesn't seem to...
2
by: Peter | last post by:
Hi, this is the code, and new row and new cell generated ok, but why the onclick and onmouseover doen't work? Thank you in advance! <html> <head> <script language="javascript"> function...
0
by: jceddy | last post by:
Hi...I hope I am posting this in an appropriate place...if not, could some direct me to someplace better? Anyway, the problem I have is that I have a page with a table with two rows. The top...
5
by: garfy | last post by:
Hi i get this error in validation Line 22 column 6: document type does not allow element "title" here. <title>Seo Web Design Los Angeles - Web Design And Search Engine Optimization L ...
1
by: mark4asp | last post by:
<form runat="server"automatically adds <divtag to code contained within. Is there a way to stop that? Mixing block-level elements with inline-level elements messes up the HTML becasuse that is...
2
by: Steve Richter | last post by:
Why would this embedded javascript not work? <span onmouseover="this.style.font-weight='bold';" >line 3</span> this does: <span onmouseover="this.style.color='blue';" >line 1</span> thanks,
1
by: Steve | last post by:
Hello! How can I realize, that when the Cursor is over an TreeNode, that the TreeNode will be underlined. I think the code must be in the "TreeNodeDataBound" Event, or? Regards Steve
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.