JavaScript – Insert Element after Specific HTML Element
To insert element in document after specific HTML Element using JavaScript, get reference to this HTML Element; call after() method on this HTML Element; and pass the element to insert, as argument to after() method.
Example
In the following example, we have HTML Element with id "myElement"
, and we shall add a paragraph element after this HTML Element using after() method.
example.html
When you click on Click Me
button in the output of HTML, a new HTML Element will be added after the HTML Element with id "myElement"
.
ADVERTISEMENT
Conclusion
In this JavaScript Tutorial, we learned how to insert an element in the document after a specific HTML Element using after() method in JavaScript.