javascript - Setting an attribute of a child <div> inside a cloned Node - Stack Overflow

I have cloned a node, but i want to set or change an attribute of a div inside that cloned node, specif

I have cloned a node, but i want to set or change an attribute of a div inside that cloned node, specifically, change the id of div id="test0"

I can't find any documentation out there on this, any straight JavaScript guys out there know a solution?

var c = document.getElementById("stone-opt0"),
cloned = c.cloneNode(true);

//CODE THAT DOESN'T WORK
cloned.getElementById("test0").id = "new-id";

What I am doing is looping through a large list of items, and placing these items into a document fragment, which i then push to the page once... I am doing this rather than adding each element to the page, then modifying after attaching to the DOM (which would be faster, no?)

I have cloned a node, but i want to set or change an attribute of a div inside that cloned node, specifically, change the id of div id="test0"

I can't find any documentation out there on this, any straight JavaScript guys out there know a solution?

var c = document.getElementById("stone-opt0"),
cloned = c.cloneNode(true);

//CODE THAT DOESN'T WORK
cloned.getElementById("test0").id = "new-id";

What I am doing is looping through a large list of items, and placing these items into a document fragment, which i then push to the page once... I am doing this rather than adding each element to the page, then modifying after attaching to the DOM (which would be faster, no?)

Share Improve this question edited Sep 14, 2012 at 20:42 Joel Grannas asked Sep 14, 2012 at 20:28 Joel GrannasJoel Grannas 2,0162 gold badges24 silver badges46 bronze badges 2
  • 1 Why in the world would you tag your question as jquery and then say "No jQuery!"... – James Montagne Commented Sep 14, 2012 at 20:29
  • i did that to try and get more javascript people to view the question. i removed it now. – Joel Grannas Commented Sep 14, 2012 at 20:36
Add a ment  | 

2 Answers 2

Reset to default 5

You're trying to call getElementById on the context of an element node. This is not possible: the getElementById method exists only on the document node (because id values have to be unique to the document). By contrast, you can do getElementsByTagName or querySelectorAll based on the context of an element.

You could, therefore, use the querySelectorAll method to do this, as long as you don't mind not supporting browsers that don't support this method, e.g. IE8.

cloned.querySelectorAll('[id="test0"]')[0].id = "new-id";

Have you tried doing cloned.getElementById("test0").id = "new-id" after you attach cloned to the DOM? getElementById() can be finicky if you're looking for something that's not part of the DOM. And when I say "finicky", I mean "generally flat out doesn't work."

You might try creating a hidden element (like a <div style="display:none">) on the page and attaching cloned to that so that it doesn't show up on the page until you're ready.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745320943a4622439.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信