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
    Oct 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to change an element in a cloned table row

    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!

  • #2
    ubh
    ubh is offline
    Regular Coder ubh's Avatar
    Join Date
    Apr 2008
    Location
    Portland, Oregon U.S.A.
    Posts
    443
    Thanks
    108
    Thanked 15 Times in 14 Posts
    Off the top of my head, I think the code should finish up like this.

    Code:
    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.
    Last edited by ubh; 10-05-2008 at 09:49 PM.

  • #3
    Smokes a Lot
    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    1,594
    Thanks
    5
    Thanked 20 Times in 20 Posts
    Like this?

    Code:
    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="";
    }
    Helping to build a bigger box. - Adam Matthews


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •