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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:12:36+00:00 2026-06-17T21:12:36+00:00

I have a condition that changes the background color of the table rows based

  • 0

I have a condition that changes the background color of the table rows based on comparing values. I want to integrate another condition that compares two cells and returns a background color on only those cells..

I already have a loop running on the table

            for (i = 0; i < rows.length; i++) {
            cells = rows[i].getElementsByTagName('td');

This is what i have but its applying the color results to the entire column rather than each cell..

         if (cells[10].innertext !== cells[11].innerText)
                cells[10].style.backgroundColor = "red";
                cells[11].style.backgroundColor = "red";

         else 
            (cells[10].innertext == cells[11].innerText)
                cells[10].style.backgroundColor = "green";
                cells[11].style.backgroundColor = "green";

thanks for the help!

  • 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-17T21:12:37+00:00Added an answer on June 17, 2026 at 9:12 pm

    You have a capitalization issue. The first innertext should be innerText. And multiple statements must be put in a block to group them for a condition.

    Also, to put a condition after else, it needs to be else if

            //-------------v
         if (cells[10].innertext !== cells[11].innerText) {
                cells[10].style.backgroundColor = "red";
                cells[11].style.backgroundColor = "red";
            //-------------v
         } else if (cells[10].innertext == cells[11].innerText) {
                cells[10].style.backgroundColor = "green";
                cells[11].style.backgroundColor = "green";
         }
    

    Although logically the second condition isn’t needed, so you could change it to:

         } else {
    

    Additionally, .textContent is the standards version of getting text from an element. If you want wider browser support, you should do something like:

    var text1 = cells[10].textContent || cells[10].innerText
    var text2 = cells[11].textContent || cells[11].innerText
    

    …and then use the variables for the comparison.


    Lastly, there’s no need to be so verbose with:

    cells = rows[i].getElementsByTagName('td');
    

    You can just use this instead:

    rows[i].cells
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a script that I want to exit early under some condition: if
I have a script that I want only to load if a certain condition
I want to change background color of the row in particular condition.My code is
I have a Word 2003 .dot template that changes its menu based on the
Sometimes I have to check for some condition that doesn't change inside a loop,
I have an $.ajax call that includes both a success and error condition: $('input[name=StateName]').live('change',
Possible Duplicate: php false place in condition I have noticed that a lot of
I have the below coding, i am make sure that if condition is TRUE,
I have an issue that looks like a race condition with a webview callback
I have a piece of Javascript that checks for a condition (via an AJAX

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.