after() method in javascript
after() method is used to insert an element or text next to the given element.
Example Explanation
- In the above example we have two div element one inside another (idDiv1, idDiv2), a button and a Javascript function is defined (fun_name()).
- When the button is clicked Javascript function is called.
- In the Javascript function, first we are getting the child div element (document.getElementById("idDiv2");) and storing in elem1 variable.
- In the next step using after() method we are adding some text next to the element (elem1.after("New Content");).
- Then we are getting the parent div element (document.getElementById("idDiv1");) and storing in elem2 variable.
- Finally using the alert method, we are displaying the whole parent content, including the text we added (alert(elem2.outerHTML);).