How can I using javascript make clone of some <div>
and set his id different from original. Jquery also will be nice.
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' .
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With