Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    973
    Thanks
    19
    Thanked 212 Times in 210 Posts
    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>

  • #3
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?


  •  

    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
    •