[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 432
  • Last Modified:

How to clone a child node with attributes

Hi.

I want to clone a node, but I am not sure how to deal with attributes. I have a structure similar to this:

<emp id="b12">Paul Anderson</emp>

I know how to clone the child nodes, but have not been able to clone the 'id' attribute.

Any help is appreciated since this is a time-sensitive project.

Thanks. J~
0
chappi77
Asked:
chappi77
  • 5
  • 4
1 Solution
 
Yury_DelendikCommented:
empNode.cloneNode(true)
0
 
chappi77Author Commented:
Yes, I got that. However, I also need to change the values of the cloned items, the attribute value in particular.

Thanks.
0
 
Yury_DelendikCommented:
what parser are you using?

0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
chappi77Author Commented:
MSXML? I'm kinda new to this part, but I think that's what you're looking for :)
0
 
Yury_DelendikCommented:
empNode.setAttribute("id", "new-value")

empNode.text = "value" or empNode.InnerText = "value"
0
 
chappi77Author Commented:
I tried that and it didn't work. Here is the function I am using:

            function addCD()
            {
                  var old = XMLdoc.getElementsByTagName("cd")[0];
                  var clone = old.cloneNode(true);

                  clone.childNodes[0].nodeValue = document.myForm.cdName.value;
                  clone.childNodes[1].firstChild.nodeValue = document.myForm.artist.value;
                  clone.childNodes[2].firstChild.nodeValue = document.myForm.price.value;

                  var tracksLength = allTracks.length;
                  var counter;
                  var myTrack;
                  var myLength;

                  for (counter = 0; counter <= tracksLength; counter++)
                  {
                        myTrack = allTracks[counter];
                        myLength = allLengths[counter];

                        if (myTrack == "0")
                        {
                              break;
                        }
                        clone.childNodes[3 + counter].firstChild.nodeValue = myTrack;
                        clone.childNodes[3 + counter].firstChild.setAttribute("length", myLength);
                  }
      
                  XMLdoc.documentElement.appendChild(clone);

                  transformDOC();
            }

I am trying to change the attribute of the track childnode to the attribute I get in my form. I get an error message returned: Object does not support this property or method... Any idea?

Thanks.
0
 
Yury_DelendikCommented:
what is clone.childNodes[3 + counter].firstChild ?
use clone.childNodes[3 + counter].firstChild.nodeTypeString
It has to be an element.
0
 
chappi77Author Commented:
clone.childNodes[3 + counter].firstChild refers to the first child (track) of the child node "cd". This works fine. The only one I can't seem to figure out is the attribute. if I use clone.childNodes[3 + counter].firstChild.nodeTypeString then it does not work at all.
0
 
Yury_DelendikCommented:
Is allLengths array?

clone.childNodes[3 + counter].firstChild.setAttribute("length", myLength);
will work if clone.childNodes[3 + counter].firstChild exists and it's element
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now