Web Analytics Made Easy -
StatCounter How to change 'class' in <A> dynamically ? - CodingForum

Announcement

Collapse
No announcement yet.

How to change 'class' in <A> dynamically ?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to change 'class' in <A> dynamically ?

    Hello,
    I have this structure :
    <a href="xxx" class="menu"> .... </a>
    How to find the object of this <a> tag from javascript and
    how to change the class of this <a> tag dynamically from javascript?

    thank you very much

  • #2
    <html>
    <head>
    <title>untitled</title>
    <style type="text/css">

    .oldclass {
    color: #00ff00;
    }

    .newclass {
    color: #ff0000;
    }

    </style>
    </head>
    <body>
    <a id="a1" class="oldclass" href="#">test</a><br><br>
    <a href="#"
    onclick="document.getElementById('a1').className='newclass'">change class</a>
    </body>
    </html>

    Comment


    • #3
      Applying Styles to Multiple ID's

      I am trying to change the color of text using Classes on specific row or rows depending on the month. There are two rows that are numbered id="11". Only the first row with id="11" is changed. What gives...?

      Please look at the following script:

      <script>
      now = new Date();
      month = now.getMonth();

      function SetStyle(){
      document.getElementById(month).className='newclass'
      }
      </script>

      <table>
      <tr id="10">
      <td>
      Blah Blah Blah
      </td>
      </tr>
      <tr id="11">
      <td>
      Blah Blah Blah
      </td>
      <td>
      Blah Blah Blah
      </td>
      </tr>
      <tr id="11">
      <td>
      Blah Blah Blah
      </td>
      <td>
      Blah Blah Blah
      </td>
      </tr>
      </table>
      Last edited by drecksau; Nov 2, 2004, 01:16 PM.

      Comment


      • #4
        getElementById is intended to reference uniquely.
        Amend your SetStyle function as follows so that you can reference by array :

        function SetStyle(){

        var totalTRs = document.getElementsByTagName("tr").length;

        for(i=0; i < totalTRs; i++)
        {
        document.getElementsByTagName("tr")[i].id == month ?
        document.getElementsByTagName("tr")[i].className='newclass' : "";
        }
        }

        Comment


        • #5
          Originally posted by Puffin the Erb
          getElementById is intended to reference uniquely.
          Amend your SetStyle function as follows so that you can reference by array :

          function SetStyle(){

          var totalTRs = document.getElementsByTagName("tr").length;

          for(i=0; i < totalTRs; i++)
          {
          document.getElementsByTagName("tr")[i].id == month ?
          document.getElementsByTagName("tr")[i].className='newclass' : "";
          }
          }
          You are the man... Thanks a bunch.


          Drecksau

          ...it said, "Windows 2000 or better." So I installed Linux...

          Comment


          • #6
            Last edited by adios; Nov 2, 2004, 03:46 PM.

            Comment

            Working...
            X