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 554173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:40:55+00:00 2026-05-13T11:40:55+00:00

I have a query building form built in PHP. The results of the query

  • 0

I have a query building form built in PHP. The results of the query are returned and placed in two different tables one above the other. (table id’s are results, and results2) Each record returned builds two tables to display its data. (Soon to be three)

I’m using the following code to change the class tags of my <tr>‘s to provide alternate row coloring:

<script type="text/javascript">
function alternate(id){
  if(document.getElementsByTagName){
    var table = document.getElementById(id);
    var rows = table.getElementsByTagName("tr");
    for(i = 0; i < rows.length; i++){
      if(i % 2 == 0){
       rows[i].className = "row-one";
       }else{
       rows[i].className = "row-two";
            }
          }
         }
        }
</script>

Then I’m using a body onload like so:

<body onload="alternate('results'); alternate('results2');">

The problem is it only colors the first two instances of these tables, when depending on the amount of records returned there could be hundreds of them.

How can I get this function to apply to every table in the document with a id = results and results2 and soon to be a results3?

  • 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-13T11:40:55+00:00Added an answer on May 13, 2026 at 11:40 am

    If you really want to do this with JavaScript, I would suggest the following code:

    First, make your tables have a class of “results” instead of ID “results1”, “results2”, etc (because as my comment on the question says, IDs must be unique, and getElementById will only return one result and apply to only one real element):

    <table class="results">...</table>
    

    Next, use this JavaScript:

    <script type="text/javascript">
    function alternate(classNameMatch) {
        var tables = document.getElementsByTagName("TABLE");
        for (var i=0; i < tables.length; i++) {
            var table = tables[i];
            if (table.className.indexOf(classNameMatch) == -1)) continue;
    
            for (var j=0; j < table.rows.length; j++) { // "TABLE" elements have a "rows" collection built-in
                table.rows[j].className = j % 2 == 0 ? "row-one" : "row-two";
            }
        }
    }
    </script>
    

    Then call alternate("results"); on page load.

    But, I really suggest doing this in PHP. JavaScript will be very inefficient with large result sets. It will also not show up right away, making the style of the page visibily change after page load.

    I would also just add a class to every other row, and then style all rows by default one way and the other class the other way:

    <style type="text/css">
    table.results tr { background-color:#f0f0f0; }
    table.results tr.row2 { background-color:#f0f0ff; }
    </style>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this query: criteria = session.CreateCriteria(typeof (Building)) .CreateAlias(Estate, estate) .SetProjection(Projections.ProjectionList() .Add(Property.ForName(Name), BuildingName) .Add(Property.ForName(estate.Name),
I have query with two MtM relations: $em = $this->getEntityManager(); $qb = $em->createQueryBuilder(); $qb
I have query table copyQuery which has Soil Condition has one of the columns,
I am building a form in access database. I have a combo box which
I'm building a page where I want to have a form that posts to
I'm building an advanced search form for my ASP.NET MVC app. I have a
I'm building a dynamic drop down box for an HTML form using PHP and
I'm building a form with radio buttons. <form id=land name=input method=POST action=test.php> <input type=radio
I have a website I'm building with two user streams. I have the code
So I'm building an app using PHP and MongoDB which will have a fair

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.