Computing.Net > Forums > Web Development > changing link text onClick

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

changing link text onClick

Reply to Message Icon

Name: jb60606
Date: September 8, 2007 at 13:03:05 Pacific
OS: Windows XP
CPU/Ram: 2.8Ghz/512MB
Product: Homeade
Comment:

Hey,

I currently have a text link that, when clicked, alters the text within the link using the following code:

a href="#" onclick="this.innerHTML='Text has been changed';">Click to change text.a

My question is; how do I get the link to change back to the original text upon a second click? I have a feeling this will need a bit more code, and I'm just not entirely familiar with javascript. Can anyone point me in the right direction?

Thanks in advance.




Sponsored Link
Ads by Google

Response Number 1
Name: Michael J (by mjdamato)
Date: September 8, 2007 at 23:08:40 Pacific
Reply:

Include the following javascript within the head of your document:

<script type="text/javascript">

oldTextAry = new Array();

function changeText (fieldObj, newTexStr) {
if (newTexStr == fieldObj.innerHTML) {
fieldObj.innerHTML = oldTextAry[fieldObj.id];
} else {
oldTextAry[fieldObj.id] = fieldObj.innerHTML;
fieldObj.innerHTML = newTexStr;
}
}

</script>

Then on your link(s) add an id parameter and call the function like this:


[a href="#" onclick="changeText(this,'New Text');" id="text1link"]Old Text[/a]

Tested in IE6 and FF2

Michael J


0

Response Number 2
Name: jb60606
Date: September 9, 2007 at 20:20:27 Pacific
Reply:

thanks, Michael

This works flawlessly.


0

Response Number 3
Name: djohndrow
Date: September 19, 2007 at 08:11:40 Pacific
Reply:

Thanks, this is perfect. All the other scripts just changed the text one time. The toggle feature is cool. =d=-


David
Web Developer


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Web Development Forum Home


Sponsored links

Ads by Google


Results for: changing link text onClick

Buttons with frames help www.computing.net/answers/webdevel/buttons-with-frames-help/528.html

links javascript instead of html www.computing.net/answers/webdevel/links-javascript-instead-of-html/2696.html

hover color, non links text? www.computing.net/answers/webdevel/hover-color-non-links-text/2969.html