insertAdjacentText() method is used to insert new text at a specific position.
Position can be represented using following values,
- 'beforebegin' : Insert the text before the specified element.
- 'afterbegin' : Insert the text inside the specified element, but before its first child.
- 'beforeend' : Insert the text inside the specified element, but after its last child.
- 'afterend' : Insert the text after the specified element.
Example - Add new Text
- If the first button is clicked, fun_before() is called. In here it adds the text just above the paragraph.
- If the second button is clicked, fun_after() is called. In here it adds the text just after the paragraph.