Easy change link colors on page w/out reload

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • michael

    Easy change link colors on page w/out reload

    I have many links on a page, but for example sake, these two:

    <a href="bla1.html " id="link1">bla1 </a>
    <a href="bla1.html " id="link2">bla1 </a>

    I use a Javascript function to change the link colors:

    document.getEle mentById("link1 ").style.color= 'lime'
    document.getEle mentById("link2 ").style.color= 'lime'

    Above works fine to change the 2 link colors to lime, but is there way to
    dynamically change all links on a page, without a separate id for each
    link, and without reloading the page with a new link style declaration?

  • Harlan Messinger

    #2
    Re: Easy change link colors on page w/out reload


    "michael" <nomail@example .com> wrote in message
    news:cb9ma4$2i4 $01$1@news.t-online.com...[color=blue]
    > I have many links on a page, but for example sake, these two:
    >
    > <a href="bla1.html " id="link1">bla1 </a>
    > <a href="bla1.html " id="link2">bla1 </a>
    >
    > I use a Javascript function to change the link colors:
    >
    > document.getEle mentById("link1 ").style.color= 'lime'
    > document.getEle mentById("link2 ").style.color= 'lime'
    >
    > Above works fine to change the 2 link colors to lime, but is there way to
    > dynamically change all links on a page, without a separate id for each
    > link, and without reloading the page with a new link style declaration?
    >[/color]

    var links = document.getEle mentsByTagName( "a");

    Comment

    • michael

      #3
      Re: Easy change link colors on page w/out reload

      Harlan Messinger wrote:
      [color=blue]
      > var links = document.getEle mentsByTagName( "a");[/color]

      By the above I've got the variable "links" as an HTML object collection.
      I suppose this is now a Javascript question; but how could I dynamically
      change all link colors on the page from here?
      Any further pointers would be much appreciated... Thanks.


      Comment

      • Mr.Clean

        #4
        Re: Easy change link colors on page w/out reload

        In article <cb9pgn$4to$05$ 1@news.t-online.com>, nomail@example. com
        says...[color=blue]
        > Harlan Messinger wrote:
        >[color=green]
        > > var links = document.getEle mentsByTagName( "a");[/color]
        >
        > By the above I've got the variable "links" as an HTML object collection.
        > I suppose this is now a Javascript question; but how could I dynamically
        > change all link colors on the page from here?
        > Any further pointers would be much appreciated... Thanks.
        >
        >
        >[/color]
        Use a for loop on the collection.

        Comment

        • michael

          #5
          Re: Easy change link colors on page w/out reload

          > Use a for loop on the collection.

          Does anyone have an example?

          Comment

          • michael

            #6
            Re: Easy change link colors on page w/out reload

            > Does anyone have an example?

            .... found a solution:

            links = document.getEle mentsByTagName( "a");
            for (var i=0;i<links.len gth;i++)
            if (links[i].className=="li nk")
            links[i].style.color = 'lime';
            }


            Comment

            • Jan Roland Eriksson

              #7
              Re: Easy change link colors on page w/out reload

              On Tue, 22 Jun 2004 18:13:43 +0200, michael <nomail@example .com> wrote:
              [color=blue]
              >I have many links on a page, but for example sake, these two:
              ><a href="bla1.html " id="link1">bla1 </a>
              ><a href="bla1.html " id="link2">bla1 </a>
              >I use a Javascript function to change the link colors:[/color]

              Why?

              --
              Rex

              Comment

              • michael

                #8
                Re: Easy change link colors on page w/out reload

                > Why?

                Why not?

                Comment

                • Neal

                  #9
                  Re: Easy change link colors on page w/out reload

                  On Wed, 23 Jun 2004 07:51:05 +0200, michael <nomail@example .com> wrote:
                  [color=blue][color=green]
                  >> Why?[/color]
                  >
                  > Why not?[/color]


                  For starters, not all users have Javascript enabled. You can change link
                  colors in CSS, which is more widely trusted.

                  Comment

                  • Brian

                    #10
                    Re: Easy change link colors on page w/out reload

                    michael wrote:
                    [color=blue][color=green]
                    >> Use a for loop on the collection.[/color]
                    >
                    > Does anyone have an example?[/color]

                    Please take it to comp.lang.javas cript. But before posting a question,
                    check the archives. This has come up before. You might also consider
                    Google.

                    --
                    Brian (remove ".invalid" to email me)

                    Comment

                    Working...