Results 1 to 6 of 6

Thread: Loop through table to get TD value?

  1. #1
    Join Date
    Aug 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Loop through table to get TD value?

    I'm trying to loop through a table to get each TD value. If the value is below a specific number then I'll do something.
    Any help is appreciated, thanks.

    Code:
    <script type="text/javascript">
    function checkForm() {
        rows = document.getElementById('tableData').getElementsByTagName('TR');
        col = rows[i].getElementsByTagName('TD');
    	for (var d = 0; d < col.length;d++) {
    		if (col[d].className == "TD") {
    			var contents = col[d].innerHTML;
    			confirm(d + ',' + contents);
    			alert("col");
    			if (col < "5")
    				alert("Less then 5");
    			if (col > 5)
    				alert("Great then 5");
    			}
    		}
    	}
    </script>
    
    <body onload="checkForm">
    <table id="tableData" name="tableData">
    	<tr>
    		<td id="TD">10</td>
    		<td id="TD">4</td>
    		<td id="TD">7</td>
    	</tr>
    	<tr>
    		<td id="TD">8</td>
    		<td id="TD">5</td>
    		<td id="TD">2</td>
    	</tr>
    	<tr>
    		<td id="TD">3</td>
    		<td id="TD">1</td>
    		<td id="TD">6</td>
    	</tr>
    </table>
     </body>
    Last edited by jscheuer1; 08-31-2012 at 06:12 AM. Reason: Format

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Code:
    <script type="text/javascript">
    function checkForm() {
    	var cols = document.getElementById('tableData').getElementsByTagName('td'), colslen = cols.length, i = -1;
    	while(++i < colslen){
    		if(cols[i].innerHTML <= 5){alert('5 or less');}
    		else {alert('Greater than 5');}
    	}
    }
    </script>
    </head>
    <body onload="checkForm();">
    <table id="tableData" name="tableData">
    	<tr>
    		<td id="TD">10</td>
    		<td id="TD">4</td>
    		<td id="TD">7</td>
    	</tr>
    	<tr>
    		<td id="TD">8</td>
    		<td id="TD">5</td>
    		<td id="TD">2</td>
    	</tr>
    	<tr>
    		<td id="TD">3</td>
    		<td id="TD">1</td>
    		<td id="TD">6</td>
    	</tr>
    </table>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Aug 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jscheuer1 View Post
    Code:
    <script type="text/javascript">
    function checkForm() {
    	var cols = document.getElementById('tableData').getElementsByTagName('td'), colslen = cols.length, i = -1;
    	while(++i < colslen){
    		if(cols[i].innerHTML <= 5){alert('5 or less');}
    		else {alert('Greater than 5');}
    	}
    }
    </script>
    </head>
    <body onload="checkForm();">
    <table id="tableData" name="tableData">
    	<tr>
    		<td id="TD">10</td>
    		<td id="TD">4</td>
    		<td id="TD">7</td>
    	</tr>
    	<tr>
    		<td id="TD">8</td>
    		<td id="TD">5</td>
    		<td id="TD">2</td>
    	</tr>
    	<tr>
    		<td id="TD">3</td>
    		<td id="TD">1</td>
    		<td id="TD">6</td>
    	</tr>
    </table>
    Thanks! That works great.

  4. #4
    Join Date
    Aug 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    That worked but I can't get it to set the bgcolor to each cell that returns the value needed. It seems to be looping through a little inconsistently and only changes the first TD's bgcolor?

    Code:
    <script type="text/javascript">
    
    var Green = "#7FFF00"
    var Yellow = "#FFFF00"
    var Red = "#DC143C"
    
    function checkForm() {
    	var cols = document.getElementById('tableData').getElementsByTagName('TD'), colslen = cols.length, i = -1;
    	while(++i < colslen){
    		if(cols[i].innerHTML <= 4){
    			alert('Red');
    			document.getElementById('TD').style.backgroundColor=Red; 
    			}
    		if(cols[i].innerHTML >= 5 || cols[i].innerHTML <= 7) {
    			alert('Yellow');
    			document.getElementById('TD').style.backgroundColor=Yellow;
    			}
    		if(cols[i].innerHTML >= 8) {
    			alert('Green');
    			document.getElementById('TD').style.backgroundColor=Green;
    			}
    	  }
    }
    </script>
    
    <table id="tableData" name="tableData" border=1>
    	<tr>
    		<td id="TD">10</td>
    		<td id="TD">4</td>
    		<td id="TD">7</td>
    	</tr>
    	<tr>
    		<td id="TD">8</td>
    		<td id="TD">5</td>
    		<td id="TD">2</td>
    	</tr>
    	<tr>
    		<td id="TD">3</td>
    		<td id="TD">1</td>
    		<td id="TD">6</td>
    	</tr>
    </table>
    Last edited by jscheuer1; 08-31-2012 at 11:06 PM. Reason: Format

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    When using getElementById in javascript there can only be one element per page with a given id. If there's more than one, browsers will either pick the first one, or throw an error. And, although your cascade of if statements is workable, it's less than optimal for the situation. This works:

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript">
    function checkForm() {
    	var cols = document.getElementById('tableData').getElementsByTagName('td'),
    	colslen = cols.length, i = -1, color = checkForm.colors, val;
    	while(++i < colslen){
    		val = cols[i].innerHTML;
    		cols[i].style.backgroundColor = val < 5? color.red : val < 8? color.yellow : color.green;
    	  }
    }
    checkForm.colors = {
    	green: "#7fff00",
    	yellow: "#ffff00",
    	red: "#dc143c"
    }
    </script>
    </head>
    <body>
    <table id="tableData" border=1>
    	<tr>
    		<td>10</td>
    		<td>4</td>
    		<td>7</td>
    	</tr>
    	<tr>
    		<td>8</td>
    		<td>5</td>
    		<td>2</td>
    	</tr>
    	<tr>
    		<td>3</td>
    		<td>1</td>
    		<td>6</td>
    	</tr>
    </table>
    <script type="text/javascript">
    checkForm();
    </script>
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Aug 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I kind of thought that was the part of the issue, thanks!!

Similar Threads

  1. While Loop in Table
    By onestopplay in forum PHP
    Replies: 4
    Last Post: 07-02-2009, 11:18 PM
  2. Replies: 3
    Last Post: 04-26-2009, 03:10 AM
  3. Replies: 5
    Last Post: 05-19-2008, 04:44 AM
  4. Replies: 4
    Last Post: 08-21-2007, 05:12 PM
  5. Dynamic Image Table Loop Advice Needed
    By alehambre in forum JavaScript
    Replies: 0
    Last Post: 05-29-2006, 10:39 PM

Bookmarks

Posting Permissions

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