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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:39:38+00:00 2026-06-16T16:39:38+00:00

I want to continuously cycle through the rows of a table, highlighting a row,

  • 0

I want to continuously cycle through the rows of a table, highlighting a row, then pausing and moving to the next row. When I reach the end I want to go back around and start at the beginning again.

I liked roXon’s answer the best because it felt more concise and functional, so I expanded it to work with three tables. But now there’s code duplication — what is the most elegant way to write the code without duplication, that allows for different tables and colors? Here’s the working three-table solution (also, is the inner function strictly necessary?):

<!doctype html>
<html>
<head>
<style>
.highlight1 { background:gold; }
.highlight2 { background:lightblue; }
.highlight3 { background:lightgreen; }
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function() {
  $(function() {
    var $TR1=$('#table1 tr:gt(0)'), TRn1=$TR1.length, c1=0;
    var $TR2=$('#table2 tr:gt(0)'), TRn2=$TR2.length, c2=0;
    var $TR3=$('#table3 tr:gt(0)'), TRn3=$TR3.length, c3=0;
    function loop(){
      $TR1.eq(c1++%TRn1).addClass('highlight1').siblings().removeClass('highlight1');
      $TR2.eq(c2++%TRn2).addClass('highlight2').siblings().removeClass('highlight2');
      $TR3.eq(c3++%TRn3).addClass('highlight3').siblings().removeClass('highlight3');
    }
    setInterval(loop, 1000);
  });
});
</script>
</head>
<body>
<table id="table1" border="1">
  <th>Table One</th>
  <tr><td>Table Row 1</td></tr>
  <tr><td>Table Row 2</td></tr>
  <tr><td>Table Row 3</td></tr>
  <tr><td>Table Row 4</td></tr>
  <tr><td>Table Row 5</td></tr>
  <tr><td>Table Row 6</td></tr>
</table>
<table id="table2" border="1">
  <th>Table Two</th>
  <tr><td>Table Row 1</td></tr>
  <tr><td>Table Row 2</td></tr>
  <tr><td>Table Row 3</td></tr>
  <tr><td>Table Row 4</td></tr>
  <tr><td>Table Row 5</td></tr>
  <tr><td>Table Row 6</td></tr>
</table>
<table id="table3" border="1">
  <th>Table Three</th>
  <tr><td>Table Row 1</td></tr>
  <tr><td>Table Row 2</td></tr>
  <tr><td>Table Row 3</td></tr>
  <tr><td>Table Row 4</td></tr>
  <tr><td>Table Row 5</td></tr>
  <tr><td>Table Row 6</td></tr>
</table>
</body>
</html>
  • 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-16T16:39:39+00:00Added an answer on June 16, 2026 at 4:39 pm

    Create a CSS class .highlight
    Be more specific while using selectors. I added a table ID

    var $TR = $('#highlight_table tr:gt(0)'),
        TRn = $TR.length,
          c = 0 ;
    
    function loop(){
       $TR.eq( c++ % TRn ).addClass('highlight').siblings().removeClass('highlight');
    }
    
    setInterval(loop, 1000);
    

    LIVE DEMO

    c++%TRn will increment our counter on each loop iteration, but thanks to the Modulo operator – once the counter and the number of TR elements matches – our counter will be reset to 0 (e.g. 4%4=0)

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

Sidebar

Related Questions

I want to continuously sample from my PC's audio line in using C# (then
I want to rotate imageview with continuously looping on android.my code rotate is working
I want to create some kind of AJAX script or call that continuously will
I have a process which gives me continuously output in Screen. I want to
Here is my script... all I want to do is have it continuously loop!
I want to continuously ping a server and see a message box when ever
I want my arrows to continuously move left and right with a delay of
I have a list of varying length that I want to continuously up date
I want my program to continuously wait for a trigger in order to perform
I want to simply have a loop so that an object continuously moves across

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.