473,472 Members | 2,143 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

change text color of a link with onclick event

I created 3 hyperlinks, when the user click each link, it will change
the color of the text of a link. For example, when user clicks Link1,
text Link1 will become red color, but Link2 and Link3 unchange. Here's
my attempts, any ideas??

<script language="javascript">
function changecolor (i)
{
document.i.fontcolor = red;
}
</script></head><a name="item1" href="test.html"
onClick='changecolor(item1)'>Link 1</a><a name="item2"
href="test.html" onclick='changecolor(item2)'>Link 2</a><a
name="item3" href="test.html" onclick='changecolor(item3)'>Link 3</a>
Jul 20 '05 #1
5 35210
Matt wrote:
I created 3 hyperlinks, when the user click each link, it will change
the color of the text of a link. For example, when user clicks Link1,
text Link1 will become red color, but Link2 and Link3 unchange. Here's
my attempts, any ideas??

<script language="javascript">
function changecolor (i)
{
document.i.fontcolor = red;
}
</script></head><a name="item1" href="test.html"
onClick='changecolor(item1)'>Link 1</a><a name="item2"
href="test.html" onclick='changecolor(item2)'>Link 2</a><a
name="item3" href="test.html" onclick='changecolor(item3)'>Link 3</a>


Hmmmm... Where do I begin...
1. You close the head, but do not create a body
2. I recommend putting the text you want to change in a FONT tag
3. The document.i technique is no good. Use getElementById or all
4. The font color needs to be "red", not red.
....

Well, here is a re-write. It works:

<HTML>
<HEAD>
<script language="javascript">
function changecolor (i)
{
if(document.getElementById)
document.getElementById(i).color = "red";
else if(document.all)
document.all[i].color = "red";

// makes it so the page does not go to test.html
return false;
}
</script>
</head>

<BODY>
<a href="test.html" onClick='return changecolor("item1")'><font
id=item1> Link 1</font></a>
<a href="test.html" onclick='return changecolor("item2")'><font
id=item2> Link 2</font></a>
<a href="test.html" onclick='return changecolor("item3")'><font
id=item3> Link 3</font></a>
</BODY>
</HTML>

Jul 20 '05 #2
Brian Genisio wrote:
2. I recommend putting the text you want to change in a FONT tag


Why? SPAN will do.
--
Bas Cost Budde
http://www.heuveltop.org/BasCB
but the domain is nl

Jul 20 '05 #3
Bas Cost Budde wrote:
Brian Genisio wrote:
2. I recommend putting the text you want to change in a FONT tag

Why? SPAN will do.

Because FONT was the first thing that came to mind, and is most
intuitive. What's wrong with FONT?

Brian

Jul 20 '05 #4
On Thu, 12 Feb 2004 08:28:10 -0500, Brian Genisio <Br**********@yahoo.com>
wrote:
Bas Cost Budde wrote:
Brian Genisio wrote:
2. I recommend putting the text you want to change in a FONT tag


Why? SPAN will do.


Because FONT was the first thing that came to mind, and is most
intuitive. What's wrong with FONT?


It's deprecated and has been for almost the past 7 years, along with
virtually every other presentational feature in HTML[1].

CSS 1 is widely supported and provides most of the functionality that
"presentational HTML" once had. CSS 2, though less supported, provides
more functionality than HTML once had.

Mike
[1] The only ones that remain can be argued to be structural elements that
have a presentational side effect.

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #5
Brian Genisio wrote:
Because FONT was the first thing that came to mind, and is most
intuitive. What's wrong with FONT?

<nagging voice> It's deprecated. </nagging voice> Nothing, really.
Although I prefer to use tags to indicate meaning. This sample is
changing font properties, so who's to judge?

BTW could you also use

<a href="test.html" onClick='return changecolor(this)'>

instead of your proposed

<a href="test.html" onClick='return changecolor("item1")'>

?

--
Bas Cost Budde
http://www.heuveltop.org/BasCB
but the domain is nl

Jul 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Matt Adams | last post by:
As well known I could specify the text color in the body tag like: <BODY TEXT=WHITE LINK=WHITE VLINK=RED ALINK=WHITE> What I want to achieve now is that always (!) the text of the last visited...
3
by: Robert | last post by:
Hi, I'm trying to make a slide show, with two frames: on the left frame small thumbnails, and on the right frame the big sized image. Of course when the user clicks on a thumbnail in the left...
4
by: Richard Cornford | last post by:
For the last couple of months I have been trying to get the next round of updates to the FAQ underway and been being thwarted by a heavy workload (the project I am working on has to be finished an...
3
by: Byron | last post by:
Hi, Javascript confuses me, so I usually limit myself to Dreamweaver's built-in scripts for stuff like imageswaps. But this time I'm trying to write something very simple myself. I do most of my...
2
by: Ben | last post by:
Hi. I have a button that change a number of images src's when I click a button. The src's are stored in an array and I just use document.src=pics to change the src of the image. However I want...
3
by: delraydog | last post by:
I'm using: document.getElementById("mylink").childNodes.nodeValue = "New Text"; To change the text value of a link that I've created. How can I do something similar to change the onclick...
15
by: Encapsulin | last post by:
Hello everybody. I'm trying to change src of quicktime embedded object with javascript: <html><body> <script language="JavaScript"> function Exchange() { document.qtvr.src = "sample2.pano";...
10
by: IchBin | last post by:
I am trying to set the state of a radio button. I do not see what I am doing wrong. Sorry, I am new at this.. I need another set of eyes to look at this snip of code. I am trying to set the radio...
3
by: jnag | last post by:
Hello, I have a website with various font options (small to large) buttons that the user can click on the banner, which runs through the site. Site contains both static and dynamic content. I...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.