Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-01-2011, 01:56 PM   PM User | #1
Rocky07
New to the CF scene

 
Join Date: Sep 2011
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Rocky07 is an unknown quantity at this point
Problem with my javascript to fetch data from HTML table

Hi,

I am new to javascript, Please help me with the issue below.
My javascript code below should actually fetch the data from html table on button click.

function displaymessage()
{
alert ("button pressed");
var table_cells = new Array();
var table7 = document.getElementById('Auth');

for (i=0,n=table7.rows.length; i < n ; i++)
{
var Rowdata = table7.rows[i];
table_cells[i] = new Array();
for(j=0,cols = Rowdata.cells.length; j < cols; j++)
{
table_cells[i,j] = Rowdata.cells[j].innerHTML;
alert (table_cells[i,j]);
}
}
alert (table_cells[1,1]);
alert (table_cells[2,1]);
alert (table_cells[3,1]);
alert (table_cells[4,1]);
alert (table_cells[5,1]);
alert (table_cells[6,1]);
alert (table_cells[7,1]);
alert (table_cells[8,1]);

}

The problem with my code above is that the statement
"alert (table_cells[i,j]);" executes properly and shows the correct value.

But the other alert statements shows only the value of the last row of the table.
i.e., my table has 9 rows. and all the alert statements shows 9th row's 2nd column's value outside the for loop. But inside the for loop it executes fine.

I tried it in IE7.

I seem to miss something. Could someone please help me out with this?

Thanks in advance.
Rocky07 is offline   Reply With Quote
Old 09-01-2011, 02:29 PM   PM User | #2
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 810
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
It should be more like this ....

Code:
<script>
function displaymessage(){
	alert ("button pressed");
	var table_cells = new Array();
	var table7 = document.getElementById('Auth');
	for (i=0,n=table7.rows.length; i < n ; i++){
		var Rowdata = table7.rows[i];
		table_cells[i] = new Array();
		for(j=0,cols = Rowdata.cells.length; j < cols; j++){
			table_cells[i][j] = Rowdata.cells[j].innerHTML;
			alert (table_cells[i][j]);
		}
	}
	alert (table_cells[0][0]);
	alert (table_cells[1][0]);
	alert (table_cells[2][0]);
	alert (table_cells[3][0]);
	alert (table_cells[4][0]);
	alert (table_cells[5][0]);
	alert (table_cells[6][0]);
	alert (table_cells[0][1]);
	alert (table_cells[1][1]);
	alert (table_cells[2][1]);
	alert (table_cells[3][1]);
	alert (table_cells[4][1]);
	alert (table_cells[5][1]);
	alert (table_cells[6][1]);
	
}

</script>
<input type="button" onclick="displaymessage()">
<table id="Auth">
	<tr>
		<td>1a
		</td>
		<td>1b
		</td>	
	</TR>
	<tr>
		<td>2a
		</td>
		<td>2b
		</td>	
	</TR>
	<tr>
		<td>3a
		</td>
		<td>3b
		</td>	
	</TR>
	<tr>
		<td>4a
		</td>
		<td>4b
		</td>	
	</TR>
	<tr>
		<td>5a
		</td>
		<td>5b
		</td>	
	</TR>
	<tr>
		<td>6a
		</td>
		<td>6b
		</td>	
	</TR>
	<tr>
		<td>7a
		</td>
		<td>7b
		</td>	
	</TR>
</table>
DaveyErwin is offline   Reply With Quote
Old 09-02-2011, 01:35 PM   PM User | #3
Rocky07
New to the CF scene

 
Join Date: Sep 2011
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Rocky07 is an unknown quantity at this point
Thanks for that DaveyErwin, using the array like you mentioned helped me.

Could you tell me why my code did not work outside the for loop?
Rocky07 is offline   Reply With Quote
Reply

Bookmarks

Tags
array, html table, javascript, multidimension, table

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:29 PM.


Advertisement
Log in to turn off these ads.