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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

insertRow() and innerHTML? 1

Status
Not open for further replies.

stormbind

Technical User
Mar 6, 2003
1,165
GB
Here's a little code to demonstrate the problem. I think it works... making it up as I go along ;)
Code:
<table>
<tr><td>Duplicate Me! :)</td></tr>
</table>

<script>
document.getElementsByTagName('TABLE')[0].insertRow();
alert(document.getElementsByTagName('TR').length);
</script>

How do you get the &quot;Duplicate Me!&quot; into the new row? :(

----------
I'm willing to trade custom scripts for... [see profile]
 
Hey...

If all you need to do is duplicate the row this will work:
Code:
<table>
<tr><td>Duplicate Me! :)</td></tr>
</table>

<script>
document.getElementsByTagName('TABLE')[0].insertRow();
var table = document.getElementsByTagName(&quot;table&quot;)[0];
var tBody = table.getElementsByTagName(&quot;tbody&quot;)[0];
var rows = tBody.getElementsByTagName(&quot;tr&quot;);    
var newRow = tBody.appendChild(rows[0].cloneNode(true));
</script>

If you need to be able to set the exact value in the new row (as opposed to just copying the previous) let me know... It'll take me a minute to remmember (or figure it out)



Travis Hawkins
BeachBum Software
travis@cfm2asp.com
 
w00t! Thanks :)

This is what I have atm, but it's a little buggy. In addition to insert rows - one can move them... and I'm finding the script is adding more TR than required.

Sometimes, moving &quot;up&quot; causes it to shimmer slightly in that direction without actually moving up.
Code:
function add(){
var table = document.getElementById('options'); 
table.insertRow();
table.getElementsByTagName('tbody')[0].appendChild(table.rows[0].cloneNode(true));
}

function up(){
var old = event.srcElement.parentElement.rowIndex;
if (old>0) {document.getElementById('options').moveRow(old--,old);}
}

----------
I'm willing to trade custom scripts for... [see profile]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top