Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6763019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:25:37+00:00 2026-05-26T14:25:37+00:00

I have the following script working to add odd and even classes to alternating

  • 0

I have the following script working to add odd and even classes to alternating table rows which works fine.

function alternate(){ 
if(document.getElementsByTagName){  
var table = document.getElementsByTagName("table");
var rows = document.getElementsByTagName("tr");
    for(i = 0; i < rows.length; i++){
  //manipulate rows   
      if(i % 2 == 0){  
        rows[i].className = "even"; 
      }else{  
        rows[i].className = "odd"; 
      }
    }
  } 
}

However a problem arises when there is more than one table on a page. I need the counter to reset for each table on the page so the first row of each table always has the same class (i.e. odd). Currently the second table on the page will just carry on counting rows odd-even so it will start on a different class if the first table has an odd number of rows.

Can anyone help me change this code to achieve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:25:38+00:00Added an answer on May 26, 2026 at 2:25 pm

    Here you go:

    function alternate() {
        var i, j, tables, rows;
    
        tables = document.getElementsByTagName( 'table' );
    
        for ( i = 0; i < tables.length; i += 1 ) {
            rows = tables[i].rows;
    
            for ( j = 0; j < rows.length; j += 1 ) {
                rows[j].className = j % 2 ? 'even' : 'odd';
            }
        }   
    }
    

    Live demo: http://jsfiddle.net/simevidas/w6rvd/


    Alternative solution:

    (Substituting the for iteration statements with forEach invocations makes the code more terse. Doesn’t work in IE8 though :/.)

    function alternate() {
        var tables = document.getElementsByTagName( 'table' );
    
        [].forEach.call( tables, function ( table ) {
            [].forEach.call( table.rows, function ( row, i ) {
                row.className = i % 2 ? 'even' : 'odd';
            });
        }); 
    }
    

    Live demo: http://jsfiddle.net/simevidas/w6rvd/1/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following script: http://jsfiddle.net/oshirowanen/8mQ7x/1/ Which works fine, but as soon as I
I have the following script, which is working for the most part Link to
I`m currently working on a script, and I have the following situation. function somnicefunction()
In tcsh , I have the following script working: #!/bin/tcsh setenv X_ROOT /some/specified/path setenv
I have the following script which first shows only the first para of the
I have the following script: Timer=0; function countdown(auctionid){ var auctions; var divs; Timer=Timer+1; if((Timer%10==0)||(Timer==1)){
I have the following script which appears multiple times on my page. I have
I have the following script: function add_fields(link, association, content) { var new_id = new
I have the following question: I am working on a JQuery script that turns
I have the following script that is working great, but I want to wrap

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.