Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clone <div> and change id

How can I using javascript make clone of some <div> and set his id different from original. Jquery also will be nice.

like image 347
Timofey Trofimov Avatar asked Aug 16 '12 10:08

Timofey Trofimov


People also ask

How do I change my ID to a cloned Div?

To clone an element and change its id: Use the cloneNode() method to clone the element. Set a different id property on the element. For example, clone.id = 'another-id' .

How to clone a div?

To clone a div and change its id with JavaScript, we can use the cloneNode method on the div. Then we can set the id property of the cloned element to set the ID. to add the div.

How do you assign an ID to a child tag of the cloned HTML?

After cloning the Html code, we are going to change the ID of child tag of <b> and <button> tag by finding the tag using its attribute name such as id, class. After finding the tag using its attribute name, then we are going change its attribute name by jQuery attr() method.


1 Answers

var div = document.getElementById('div_id'),     clone = div.cloneNode(true); // true means clone all childNodes and all event handlers clone.id = "some_id"; document.body.appendChild(clone); 
like image 91
Sergii Stotskyi Avatar answered Oct 02 '22 14:10

Sergii Stotskyi