It only takes a few lines of Javascript to make a HTML table start rotating it’s rows. Example:

Demo 1

field 1a field 1b
field 2a field 2b
field 3a field 3b
field 4a field 4b
field 5a field 5b
field 6a field 6b
field 7a field 7b
field 8a field 8b

Demo 2 – Job board

Web designer
£140.00
Start Up Stationary Design
£80.00
Flash / XML Menu System
£400.00
Open Source Application Rebranding
£10.00
Website Design/Build
£50.00
Internet Marketing Guru
£40.00
Icon design – Urgent project
£40.00
Looking for a dragon slayer
£30.00
Poster Design
£460.00
Telesales contacting schools
£40.00
Save the cheerleader
£40.00
Recruiting, need a honest liar
£403.00
Chess grand master wanted
£440.00
Gringots gold for sale
£430.00
webmaster of the universe
£240.00
sale of widgets
£450.00
Will code for food
£420.00
waterproof fish required
£4520.00
blind waiter needed
£240.00
fire resistant cigaretes?
£420.00

Using javascript, you need to remove the last row of the table and then insert it in front of the first table row and then repeat continuously.

Demo 1

We give the tbody an id of “myTableBody” so we can grab it with document.getElementById("myTableBody") and use the javascript firstChild and lastChild selectors to reference the top and bottom rows. To loop continuously, we use code>setInterval.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
	<title>Rotating table example</title>
	<script type="text/javascript">
	function poprows()
	{
		var tableBody = document.getElementById("myTableBody");
		tableBody.insertBefore(tableBody.removeChild(tableBody.lastChild), tableBody.firstChild);
	}
	var poprowstimer = setInterval('poprows()', 1000);
	</script>
</head>
<body>
	<h2>A rotating Table</h2>
	<table>
	<tbody id="myTableBody">
	<tr><td>field 1a</td><td>field 1b</td></tr>
	<tr><td>field 2a</td><td>field 2b</td></tr>
	<tr><td>field 3a</td><td>field 3b</td></tr>
	<tr><td>field 4a</td><td>field 4b</td></tr>
	<tr><td>field 5a</td><td>field 5b</td></tr>
	</tbody>
	</table>
</body>
</html>

Demo 2 - Job board

There are two main changes from demo 1:

  1. We use some JQuery to give us a fade in effect. In my opinion, using Jquery makes the Javascript more readable and maintainable.
  2. We start the table with only the first few rows visible using overflow:hidden. This trick gives the illusion that fresh content is continuously scrolling on the page a.k.a a vertical ticker.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
	<title>Example</title>
	<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
	<script type="text/javascript">
	function poprows()
	{
		var newRow = $('#resultsTable tr:last').remove(); // Remove last row
		$('td div',newRow).hide();                        // Hide inner row divs
		$('#resultsTable tr:first').before(newRow);       // Add row as first
		$('td div',newRow).fadeIn(2500);                  // Show inner row divs slowly
	}

	$(document).ready(function()
	{
		var poprowstimer = setInterval('poprows()', 1000);
	});
	</script>
</head>
<body>
  <h2>A Job Board</h2>
  <div style="height:275px;overflow:hidden">
	<table id="resultsTable">
	<tr><td><div>Web designer</div></td><td><div>&pound;140.00 Fixed Fee</div></td></tr>
	<tr><td><div>Start Up Stationary Design</div></td><td><div>&pound;80.00 Variable Fee</div></td></tr>
	<tr><td><div>Flash / XML Menu System</div></td><td><div>&pound;400.00 Fixed Fee</div></td></tr>
	<tr><td><div>Website Design/Build</div></td><td><div>&pound;50.00 Variable Fee</div></td></tr>
	<tr><td><div>Internet Marketing Guru</div></td><td><div>&pound;40.00 Variable Fee</div></td></tr>
...
	<tr><td><div>webmaster of the universe</div></td><td><div>&pound;240.00 Fixed Fee</div></td></tr>
	</table>
  </div>
</body>
</html>

2 Responses to Using Javascript to make a table rotate

  1. Khan says:

    Its cool. But is it possible to make table refreshable with in this code.

  2. Angie says:

    For demo 1 how do you stop the rows from rotation using onmouseover?

Leave a Reply