Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > if no "onmouseover" in HTML, how can javascript do an link id - dependent action?

Reply
Thread Tools

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

 
 
metasilk
Guest
Posts: n/a
 
      09-01-2004
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
 
Reply With Quote
 
 
 
 
Michael Winter
Guest
Posts: n/a
 
      09-01-2004
On 1 Sep 2004 06:42:31 -0700, metasilk <> 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.
 
Reply With Quote
 
 
 
 
Mike Foster
Guest
Posts: n/a
 
      09-04-2004
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
non-dependent vs. dependent template names puzzlecracker C++ 1 08-07-2008 07:42 AM
RE: Link Link Link =?Utf-8?B?REw=?= Windows 64bit 0 05-17-2005 12:15 PM
Re: Link Link Link DANGER WILL ROBINSON!!! Kevin Spencer ASP .Net 0 05-17-2005 10:41 AM
defining a flag-dependent constant valentin tihomirov VHDL 3 11-28-2004 04:15 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57