1<!DOCTYPE html>
2<html>
3<body>
4
5<h1>HTML DOM insertAdjacentElement() Method</h1>
6
7<p>This method inserts a specified element, into a specified position.</p>
8
9<span style="color:red">My span</span>
10
11<h2 id="myH2">My Header</h2>
12
13<p>Click the button to insert the red span after the header:</p>
14
15<button onclick="myFunction()">Move span</button>
16
17<script>
18function myFunction() {
19 var s = document.getElementsByTagName("span")[0];
20 var h = document.getElementById("myH2");
21 h.insertAdjacentElement("afterend", s);
22}
23</script>
24
25</body>
26</html>
1<!DOCTYPE html>
2<html>
3<body>
4
5<h1>HTML DOM insertAdjacentHTML() Method</h1>
6
7<p>This method inserts a specified text as HTML, into a specified position in the document.</p>
8
9<h2 id="myH2">My Header</h2>
10
11<p>Click the button to insert a paragraph after the header:</p>
12
13<button onclick="myFunction()">Insert a paragraph</button>
14
15<script>
16function myFunction() {
17 var h = document.getElementById("myH2");
18 h.insertAdjacentHTML("afterend", "<p>My new paragraph</p>");
19}
20</script>
21
22</body>
23</html>