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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:19:06+00:00 2026-06-01T16:19:06+00:00

I currently have an alternative table row color with the following javascript code: function

  • 0

I currently have an alternative table row color with the following javascript code:

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 = "odd0"; 
            }else { 
                rows[i].className = "even2"; 
            }
        }
    }
} // end function

What I would like to add in here, is to make the condition of either of the following:

  • If Div id “arrow” exists in the table row, then make the classname “odd0”

  • If Div id “arrow” exists in the table row, then make the classname the same color as the row above it.

My first attempt was to try to make the classname “odd0” if the div id “arrow” exists; however, it just breaks my code or doesn’t read it.

I attempted the following, by either making it into its own function or inserting it within the for loop…

 if (document.getElementById("arrow")) {
  rows[i].className = "odd0";
 }

Any help is much appreciated!

  • 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-06-01T16:19:07+00:00Added an answer on June 1, 2026 at 4:19 pm

    Your question is a bit vague and your requirements seem contradictory. But, I think what you want is for your rows to alternate between odd0 and even2, unless you have an arrow in your row. Then you want the current row to have the same class as the row above it.

    Correct me if I am wrong.

    Because you put jQuery as a tag, I will give you a jQuery answer. If you don’t know jQuery, I would recommend learning it. It makes this kind of class/css manipulation a lot easier than straight JavaScript, and takes care of all of the cross-browser issues for you.

    function alternate(id) {
        var i = 1;
        // Go through each row
        $('#'+id).find('tr').each(function() {
            // If this isn't an "arrow" row, increment the count
            if ($(this).find('.arrow').length < 1) {
                i++;
            }
            // Decide whether the row is odd or even
            if (i % 2 == 0) {
                $(this).addClass('even2');
            } else {    
                $(this).addClass('odd0');
            }
        });
    }
    

    Demo: http://jsfiddle.net/jtbowden/uBgYH/1/

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

Sidebar

Related Questions

I'd like to have a function that will highlight row of the table when
I currently have a java script solution to make an entire table row clickable.
I currently have this code use for printing a table in mysql database ,
I have a table with the following fields: id VARCHAR(32) PRIMARY KEY, parent VARCHAR(32),
I currently have database setup like so: CREATE TABLE `article` ( `id` int(11) NOT
I have the following schema: TABLE bands -> band_id -> property1 -> property2 ->
I have the following code that uses Sequence objects to read data from a
I'm trying to create a table-like object within an SVG document. Currently, due to
I have an XSLT where I want to alternate the row colors of an
I currently have a notifications table in rails which contains the columns: notification_type, notification_id

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.