From the course: JavaScript: Enhancing the DOM
Unlock the full course today
Join today to access over 24,000 courses taught by industry experts.
Cloning, replacing, and removing nodes
From the course: JavaScript: Enhancing the DOM
Cloning, replacing, and removing nodes
- [Narrator] Sometimes you may want to duplicate, replace or remove elements. Let's start with clone. To clone an elements you should use cloneNode. It duplicates the node. You can set it to either ignoring any child nodes or also cloning those as well. element.cloneNode(true) clones the element and its children. While false clones only the element itself. Here we have a simple webpage again. On line 14 we have a task list. It has two linked items. The first one is Complete Financial Review. The second one is Update Project Timeline. In our JavaScripts, we're going to select a task list and we're going to say that we want to clone the first element child of task list, meaning our first list item. So we go ahead and say cloneNode with true. This way it would also clone any children. And then we overwrite the text content to saying Prepare Quarterly Report. Very important, we need to append it as well. Otherwise it will not show up. And on the webpage, Prepare Quarterly Report is now…