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.
|