www.webdeveloper.com
Results 1 to 2 of 2

Thread: Can someone explain to me this function of striping tables and how it works?

  1. #1
    Join Date
    Oct 2012
    Posts
    1

    Can someone explain to me this function of striping tables and how it works?

    The HTML:
    HTML Code:
    <!DOCTYPE html>
    <html lang="en">
    	<head>
    		<link rel="stylesheet" type="text/css" href="refresh.css"/>
    		<script type="text/javascript" src="refresh.js"></script>
    	</head>
    	<body>
    		<table>
    		<caption>Itinerary</caption> 
    			<thead> 
    				<tr>
    				<th>When</th>
    				<th>Where</th> </tr>
    			</thead> 
    			<tbody> 
    				<tr>
    					<td>June 9th</td>
    					<td>Portland, <abbr title="Oregon">OR</abbr></td>
    				</tr>
    				<tr> 
    					<td>June 10th</td> 
    					<td>Seattle, <abbr title="Washington">WA</abbr></td>
    				</tr> 
    				<tr>
    					<td>June 12th</td>
    					<td>Sacramento, <abbr title="California">CA</abbr></td> 
    				</tr>
    			</tbody> 
    		</table>
    	</body>
    </html>	
    Here's the JS code:
    Code:
    function stripeTables() { 
    	if (!document.getElementsByTagName) return false; 
    	var tables = document.getElementsByTagName("table");
    	var odd, rows; 
    	for (var i=0; i<tables.length; i++) {
    		odd = false;
    		rows = tables[i].getElementsByTagName("tr"); 
    		for (var j=0; j<rows.length; j++) {
    		if (odd == true) {
    			rows[j].style.backgroundColor = "#ffc";
    			odd = false; 
    		} else {
    			odd = true;
    		}
    		}
    	}
    }

    Thanks alot everyone

  2. #2
    Join Date
    Oct 2012
    Posts
    36
    function stripeTables() {

    if (!document.getElementsByTagName) return false; // check to see if browser supports the 'getElementsByTagName' method, if not the function exits here

    var tables = document.getElementsByTagName("table"); // builds an array of all the tables in the current page

    var odd, rows;
    for (var i=0; i<tables.length; i++) { // iterates through tables in page
    odd = false; // variable used for row alternation
    rows = tables[i].getElementsByTagName("tr"); // gets rows in current table
    for (var j=0; j<rows.length; j++) { // iterates through rows

    if (odd == true) { //if the row is odd-numbered, add background colour
    rows[j].style.backgroundColor = "#ffc";
    odd = false; // next row will be even-numbered
    } else {
    odd = true; // next row will be odd-numbered
    }
    }
    }
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles