...

View Full Version : How to change an element in a cloned table row



ssantos
10-03-2008, 12:19 AM
Hi - I am trying to clone a row in a table. This row contains a form field which could be a text field or a drop-down box. That field could contain a value. What I would like to do is clone the row (since it has a description of the field) - but blank out the data if it exists.

Here is the code I have so far:

var myTable= myBody.getElementsByTagName("TABLE").item(0);
var myTR = myTable.getElementsByTagName("TR").item(1);
var myNewTR = myTR.cloneNode(true);

Within the myTR is a field called "someval". What is the best way to access this field to set its value to blank - taking into account that sometimes this field is of input type = text or it could be a drop-down box? Would I have to access it via childNodes?

Thanks in advance for your help!

ubh
10-05-2008, 10:11 PM
Off the top of my head, I think the code should finish up like this.



var myTable = document.getElementsByTagName("TABLE").[0];
var myTR = document.getElementsByTagName("TR").[1];
var myNewTR = myTR.cloneNode(true);
myTable.appendChild(myNewTR);


Note that you must point your variables to document first before trying to point to a particular elements attribute or even the node its self.

Also once you have the element cloned you have to append the cloned node to the table.

Script is not tested.

Basscyst
10-11-2008, 12:57 AM
Like this?



var myTable= myBody.getElementsByTagName("TABLE").item(0);
var myTR = myTable.getElementsByTagName("TR").item(1);
var myNewTR = myTR.cloneNode(true);

var sel=myNewTR.getElementsByTagName('select');
var inp=myNewTR.getElementsByTagName('input');
if(sel.length>0){
sel[0].selectedIndex=0;
}else if(inp.length>0){
inp[0].value="";
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum