Last updated: Mar 5, 2024
Reading time·3 min
insertAdjacentText()
To append text to an element:
document.createTextNode()
method to create a text node.appendChild()
method to append the text node to the element.appendChild
method will add the text node to the end of the element's
list of children.Here is the HTML for the examples.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>bobbyhadz.com</title> </head> <body> <div id="box"> bobby <span style="background-color: salmon">hadz</span> </div> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
// ✅ Append text to element const box = document.getElementById('box'); const text = document.createTextNode(' tutorial'); box.appendChild(text);
We selected the element we want to append text to using the
document.getElementById()
method.
The next step is to create a text node using the document.createTextNode() method.
The method takes a string and creates a text node from it.
The last step is to use the Node.appendChild() method to append the text node to the end of the element's children.
This approach can be used to append text to any element, e.g. div
, span
,
paragraph, etc.
Alternatively, you can use the insertAdjacentText() method.
insertAdjacentText()
You can also use the insertAdjacentText
method.
The method takes a position and a string as parameters and inserts a new text node at the provided position relative to the element it was called on.
const box = document.getElementById('box'); box.insertAdjacentText('beforeend', ' new text');
The insertAdjacentText() method takes the following 2 parameters:
position
- the position relative to the element where the text should be
inserted. Can be one of the following 4:beforebegin
- before the element itself.afterbegin
- just inside the element, before its first child.beforeend
- just inside the element, after its last child.afterend
- after the element itself.data
- the string from which to create a new text node to insert at the
given position.You can use the insertAdjacent()
method to append text to an element of any
type, e.g. div
, span
, paragraph.
Which approach you pick is a matter of personal preference. I'd go with the
insertAdjacentText
method as it is more concise and direct.
If you need to append HTML
to an element and not just text, use the
insertAdjacentHTML
method.
const box = document.getElementById('box'); box.insertAdjacentHTML( 'beforeend', '<span style="background-color: yellow"> New html</span>', );
The insertAdjacentHTML() method takes the following 2 parameters:
position
- the position relative to the element where the HTML should be
inserted. Can be one of the following:
beforebegin
- before the element itself.afterbegin
- just inside the element, before its first child.beforeend
- just inside the element, after its last child.afterend
- after the element itself.text
- the string to be parsed as HTML and inserted into the DOM.
It should be noted that you shouldn't append HTML that is from user-generated input without escaping it.
In the example, we inserted text just inside the div
element, after its last
child, but you can change the first argument depending on your use case.