Click to See Complete Forum and Search --> : Referencing Anchor elements


mjbarfoot
04-24-2003, 08:34 AM
Hi,
I'm having some difficulty referencing Anchor elements in my script.

From a mouseover event on a image I want to change the text style of a link from text-decoration:none to text-decoration:underline.

I presumed I would be able to something like this.

<a href="#" onMouseOver="mylink.style.text-decoration:underline"...

<a ID="mylink" style="text-decoration:none"...>test</a>

You get the general idea...anyway whatever I try I just get the usual "object expected" message. I'm not sure why the first link cannot refer to the second. I have tried using a full declaration something like. document.body.mylink... but this does not work either.

Guess I'm missing something fairly crucial here. Any ideas??

Thanks in advance,
Matt

pyro
04-24-2003, 08:38 AM
You were close... Try it like this: (changes in bold)

<a href="#" onMouseOver="document.getElementById('mylink').style.textDecoration='underline'; return false;">test1</a>

<a id="mylink" style="text-decoration:none">test2</a>

Charles
04-24-2003, 09:45 AM
Under the old Document Object Model (DOM) anchors and links could not be referenced by name, you would have to use something like:

document.anchors[4] or document.links[4]

However and for some strange reason, until JavaScript 1.2 all the elements of the anchors array were always null.

Keep in mind that only about 88% of browsers out there will understand the old DOM method and that fewer even than that will understand pyro's new DOM method.

mjbarfoot
04-24-2003, 10:05 AM
Cheers Pyro for the answer. I knew it was something simple.

Also Thanks Charles. I know what you are saying and what I'm doing probably isn't too friendly to some of the older browsers, but then if it just doesn't work that's cool. The links will still work anyway.

Surely most of the surfing population will be using a browser that support the new DOM model now??

Short of installing multiple browsers what's the best way to check this? I know netmechanic used to do it for free, but now they charge for this.

Regards,
Matt

pyro
04-24-2003, 10:09 AM
[edit]

Global stats can be found here: http://www.thecounter.com/stats/