How to change the color of all links on mouseover.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hendsome
    New Member
    • May 2010
    • 5

    #1

    How to change the color of all links on mouseover.

    The link and visited properties of the links on my web pages are the same color as text (black), while hover and active properties are green.

    In order to help the user get the overall picture of the page links, I need JavaScript that will use an area or text (e.g. "Show all the links on this page.") to colorize (e.g. in blue) all the links on the page on mouseover, and return the regular link properties on mouse-out.
    Last edited by Niheel; May 18 '10, 06:20 PM. Reason: removed stuff that not question details
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    #2
    Code:
    document.getElementById('show_all_links').onmouseover = function() {
        var a = document.getElementsByTagName('a');
        for (var i = 0; i <a.length; i++) if (a[i].href != "") a[i].style.color = 'blue';
    }
    .... similarly for onmouseout.

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      a more CSS oriented solution could work like the following:
      prepare the highlight of all <a> by a container class (which you don’t set by default).
      Code:
      .highlight a {
          /* your highlight CSS */
      }
      with onmouseover, you set this class name to the document body, so that the CSS kicks in.
      Code:
      // execute on mouseover
      function enlighten()
      {
          document.body.className = "highlight";
      }
      undo this on mouseout.

      Comment

      • hendsome
        New Member
        • May 2010
        • 5

        #4
        Originally posted by hsriat
        Code:
        document.getElementById('show_all_links').onmouseover = function() {
            var a = document.getElementsByTagName('a');
            for (var i = 0; i <a.length; i++) if (a[i].href != "") a[i].style.color = 'blue';
        }
        .... similarly for onmouseout.
        Hi hsriat,

        Thanks!
        This works fine.

        In case there are some pre-styled links on the web page besides the black links, would it be possible to make the function scan the page, pick up and store all of the original link styles (for link, visited, hover, active states), highlight all the links on the page in a desired color (e.g. blue), and restore all the original link styles on mouseout?

        Regards,

        Hendsome

        Comment

        • hendsome
          New Member
          • May 2010
          • 5

          #5
          Originally posted by Dormilich
          a more CSS oriented solution could work like the following:
          prepare the highlight of all <a> by a container class (which you don’t set by default).
          Code:
          .highlight a {
              /* your highlight CSS */
          }
          with onmouseover, you set this class name to the document body, so that the CSS kicks in.
          Code:
          // execute on mouseover
          function enlighten()
          {
              document.body.className = "highlight";
          }
          undo this on mouseout.
          Hi Dormilich,

          Your suggestion looks cool, and I suppose it would preserve the pre-styled links on the page, but it only highlights the line that is used for mouseover.

          Regards,

          Hendsome

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            but it only highlights the line that is used for mouseover.
            where did you apply the class to?

            Comment

            • hendsome
              New Member
              • May 2010
              • 5

              #7
              Originally posted by Dormilich
              where did you apply the class to?
              Code:
              <style type="text/css">
              .highlight a {
              	color: #039;
              }
              </style>
              <body>
              <script language="JavaScript">
              function enlighten()
              {
                  document.body.className = "highlight";
              }
              function normal()
              {
                  document.body.className = "";
              }
              </script>
              <a href="#" onMouseOver="enlighten();" onMouseOut="normal()">Mouseover to see the links on this page.</a>
              ...
              </body>

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                well, it works for me …

                Comment

                • hendsome
                  New Member
                  • May 2010
                  • 5

                  #9
                  Originally posted by Dormilich
                  well, it works for me …
                  I tested it again. It works all right.
                  Thanks.

                  Comment

                  Working...