change text color of a link with onclick event

Discussion in 'Javascript' started by Matt, Feb 10, 2004.

  1. Matt

    Matt Guest

    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>
    Matt, Feb 10, 2004
    1. Advertisements

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

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

    // makes it so the page does not go to test.html
    return false;

    <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>
    Brian Genisio, Feb 11, 2004
    1. Advertisements

  3. Why? SPAN will do.
    Bas Cost Budde, Feb 12, 2004

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

    Brian Genisio, Feb 12, 2004
  5. <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, Feb 12, 2004
  6. 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.


    [1] The only ones that remain can be argued to be structural elements that
    have a presentational side effect.
    Michael Winter, Feb 12, 2004
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.