Web Analytics Made Easy -
StatCounter Problem with my javascript to fetch data from HTML table - CodingForum

Announcement

Collapse
No announcement yet.

Problem with my javascript to fetch data from HTML table

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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
    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>

    Comment


    • #3
      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?

      Comment

      Working...
      X