Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add New Row to Table 2

Status
Not open for further replies.

vicvirk

Programmer
Feb 4, 2009
636
CA
This works find in FF and Chrome (PC), but not in IE (I have 7 installed)...IE gives its usual descriptive error: "Uknown Runtime error" on the line highlighted in red...

Will it simply not work this way, do I have to use the DOM as outlined on this link (as an example):


Code:
<script language="javascript">
	function addRowToTable() {
		var currentRows = document.getElementById("myTable").innerHTML;
		var newRow = "";
		newRow = "<tr><td>new col 1</td><td>new col 2</td></tr>";
		[COLOR=red]document.getElementById("myTable").innerHTML = currentRows + newRow;[/color]
	}
</script>
<a href="javascript:addRowToTable()">click</a>
<table id="myTable">
<tr>
	<td>orig col 1</td>
	<td>orig col 2</td>
</tr>
</table>

--------
GOOGLE is a great resource to find answers to questions like "how do i..."

If you don't know exaclty what you want to do or what to search on, try Google Suggest: --------
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javasc
 
Try this:

Code:
 var currentRows = document.getElementById("myTable").insertRow(3);
        var firstcell=currentRows.insertCell(0);
        var secondcell=currentRows.insertCell(1);
        
        firstcell.innerHTML = "new col 1";
        secondcell.innerHTML = "new col 2";


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thanks Phil, that led me in the right direction..

Any idea why the other method I posted above didn't work in IE?

--------
GOOGLE is a great resource to find answers to questions like "how do i..."

If you don't know exaclty what you want to do or what to search on, try Google Suggest: --------
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javasc
 
>Any idea why the other method I posted above didn't work in IE?
It is due to this line.
>document.getElementById("myTable").innerHTML = currentRows + newRow;
The innerHTML of table object is readonly on IE.
 
cool - thanks tsuji

--------
GOOGLE is a great resource to find answers to questions like "how do i..."

If you don't know exaclty what you want to do or what to search on, try Google Suggest: --------
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javasc
 
I would also suggest you alert the currentRows which is the original innerHTML of the table. You'll see there is a tbody node coming from the inner-building mechanism of IE as well as mozilla/... The build of table would be safer using table object model (tob) for cross-browser compatibility purpose.
 
Yeah, I used Phil's method and it worked great...I'm not too concerned about browsers as the end product is something only me and a few friends are going to use...as long as it works in IE, I'm happy - if it is something I'm going to release to the rest of the world, then yeah, I would look at a better approach.



--------
GOOGLE is a great resource to find answers to questions like "how do i..."

If you don't know exaclty what you want to do or what to search on, try Google Suggest: --------
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javasc
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top