Change link text

C

contact

I need to change all the links on my site that say "more information"
to something else. Unfortunately I don't have access to change the
component that places these links in the first place, so I need to use
Javascript at runtime to go through, find each instance of a link with
the above string and change it. Is there a good cross-browser way of
achieving this? Is HTMLAnchorElement a good place to start?

Thanks
 
W

web.dev

I need to change all the links on my site that say "more information"
to something else. Unfortunately I don't have access to change the
component that places these links in the first place, so I need to use
Javascript at runtime to go through, find each instance of a link with
the above string and change it. Is there a good cross-browser way of
achieving this? Is HTMLAnchorElement a good place to start?

Thanks

This should give you a start. It's very simple and not very
comprehensive. This sample really works in the case such as:

<a href = "uri">more information</a>

sample script:

var anchors = document.getElementsByTagName("a");

for(var i = 0; i < anchors.length; ++i)
{
if(anchors.firstChild.data == "more information")
{
anchors.replaceChild(document.createTextNode("new info"),
anchors.firstChild);
}
}
 
G

Gérard Talbot

web.dev a écrit :
This should give you a start. It's very simple and not very
comprehensive. This sample really works in the case such as:

<a href = "uri">more information</a>

sample script:

var anchors = document.getElementsByTagName("a");

Also
var arrLinks = document.links;
http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-7068919
for(var i = 0; i < anchors.length; ++i)

it should be i++, not ++i
{
if(anchors.firstChild.data == "more information")
{
anchors.replaceChild(document.createTextNode("new info"),
anchors.firstChild);
}
}


for(var LinkIterator = 0; LinkIterator < arrLinks.length; LinkIterator++)
{
if(arrLinks[LinkIterator].firstChild.nodeValue == "more information")
{
arrLinks[LinkIterator].firstChild.nodeValue = "new info";
};
};

Gérard
 
R

Richard Cornford

Gérard Talbot said:
web.dev a écrit :

it should be i++, not ++i
<snip>

There is no should here. In context pre-increment and post-increment
will have the same outcome, and pre-increment is theoretically the more
efficient operator.

Richard.
 
C

contact

Thanks all, for your help. I'm getting an object required error on this
line:

if(arrLinks[LinkIterator].firstChild.nodeValue == "more information")

I've added this code to the bottom of my page. Any ideas?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top