Skip to content

JavaScript adds element after another | Example code

  • by

Use the before method to add elements after another element in JavaScript.

Note: Before and after are both marked as “experimental” on MDN page: Experimental. Expect behavior to change in the future.

JavaScript add element after another HTML example

HTML example codes:-

Insert p tag after div tag

<!DOCTYPE html>

    <div id="div1">Hello world</div>

        // Parent Element
        const el = document.getElementById("div1");

        // creating new element 
        const newEl = document.createElement("p"); = "foo";


        // Adding text in new p tag
        document.getElementById("foo").innerHTML = "I am new tag";




JavaScript adds element after another html

Read more:

Do comment if you have any doubts and suggestions on these JS codes.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading