Modifying the Document
Browser Document: Modifying the Document
How do you create new DOM nodes in JavaScript?
View Answer:
let newNode = document.createElement('div');
newNode.textContent = 'Hello!';
document.body.appendChild(newNode);
How do you place a DOM node into an HTML document using JavaScript?
View Answer:
<ul id="menu">
</ul>
<script>
function createMenuItem(name) {
let li = document.createElement('li');
li.textContent = name;
return li;
}
// get the ul#menu
const menu = document.querySelector('#menu');
// add menu item
menu.appendChild(createMenuItem('Home'));
menu.appendChild(createMenuItem('Services'));
menu.appendChild(createMenuItem('About Us'));
</script>
How do you insert HTML into a document using JavaScript?
View Answer:
Additional Information: We can also use insertAdjacentText and insertAdjacentElement in a similar fashion.
- The insertAdjacentText(where, text) has the same syntax, but a string of text is inserted “as text” instead of HTML.
- The insertAdjacentElement(where, element) has the same syntax but inserts an element.
- They exist primarily to make syntax "consistent." In practice, insertAdjacentHTML is the sole method utilized most of the time. Because we have methods append/prepend/before/after for elements and text - they are easy to create and may incorporate nodes/text fragments.
<div id="div"></div>
<script>
div.insertAdjacentHTML('beforebegin', '<p>Hello</p>');
div.insertAdjacentHTML('afterend', '<p>Bye</p>');
</script>
<!-- OUTPUT RESULT -->
<p>Hello</p>
<div id="div"></div>
<p>Bye</p>
Is it possible to remove a DOM node using JavaScript?
View Answer:
<style>
.alert {
padding: 15px;
border: 1px solid #d6e9c6;
border-radius: 4px;
color: #3c763d;
background-color: #dff0d8;
}
</style>
<script>
let div = document.createElement('div');
div.className = 'alert';
div.innerHTML =
"<strong>Hi there!</strong> You've read an important message.";
document.body.append(div);
setTimeout(() => div.remove(), 1000);
</script>
Can you swap elements using JavaScript?
View Answer:
<div id="first">First</div>
<div id="second">Second</div>
<script>
const first = document.getElementById('first');
const second = document.getElementById('second');
// no need to call remove
second.after(first); // take #second and after it insert #first
</script>
<!-- Output: -->
<!-- Second -->
<!-- First -->
Can you clone an element node in JavaScript?
View Answer:
<style>
.alert {
padding: 15px;
border: 1px solid #d6e9c6;
border-radius: 4px;
color: #3c763d;
background-color: #dff0d8;
}
</style>
<div class="alert" id="div">
<strong>Hi there!</strong> You've read an important message.
</div>
<script>
let div2 = div.cloneNode(true); // clone the message
div2.querySelector('strong').innerHTML = 'Bye there!'; // change the clone
div.after(div2); // show the clone after the existing div
</script>
Once you have cloned the node, you use the appendChild() or insertBefore() method to insert the cloned node into the document. If you want a deep clone, you set the deep parameter value to true if you want to clone all descendants (children); otherwise, false.
Can you explain the function and syntax of the DocumentFragment Object?
View Answer:
<ul id="ul"></ul>
<script>
function getListContent() {
let fragment = new DocumentFragment();
for (let i = 1; i <= 3; i++) {
let li = document.createElement('li');
li.append(i);
fragment.append(li);
}
return fragment;
}
ul.append(getListContent()); // (*)
</script>
<!-- Output:
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul> -->
We rarely use DocumentFragment explicitly. Why append to a special kind of node if we can return an array of nodes instead?
At some point in your career, you run across older methods of inserting and removing elements. Can you explain what these methods are and why you should know them?
View Answer:
<ol id="list">
<li>0</li>
<li>1</li>
<li>2</li>
</ol>
<script>
let newLi = document.createElement('li');
newLi.innerHTML = 'Hello, world!';
list.appendChild(newLi);
</script>
<!--
Output:
1. 0
2. 1
3. 2
4. Hello, world!
-->
These methods come from ancient times. Nowadays, there is no reason to use them, as modern methods, such as append, prepend, before, after, remove, replaceWith, are more flexible. This information helps to understand old scripts, but now we do not need it for new development.
Can you explain the function of the document.write() method?
View Answer:
Syntax: document.write(markup);
<p>After one second the contents of this page will be replaced...</p>
<script>
// document.write after 1 second
// that's after the page loaded, so it erases the existing content
setTimeout(() => document.write('<b>...By this.</b>'), 1000);
</script>
Because document.write() writes to the document stream, calling document.write() on a closed (loaded) document automatically calls document.open(), which will clear (overwrite) the document. The method comes from times when there was no DOM, no standards… Old times. It still lives because scripts are using it. In modern scripts, we can rarely see it because If we call it afterward, the existing document content gets erased. There are some use cases for this, but very few.