JavaScript/Restructure DOM/Exercises

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Topic: Restructure the DOM tree



1. We use the HTML page of the previous page for the exercises. Write a script that moves the two <div> sections BEFORE the button.

Click to see solution
function show() {
  "use strict";

  // select the body element. It's the parent.
  const body = document.getElementById("body");
  // select the button element
  const button = document.getElementById("buttonShow");
  // select the 'div' elements
  const div_1 = document.getElementById("div_1");
  const div_2 = document.getElementById("div_2");

  // move the div elements BEFORE the button
  body.insertBefore(div_1, button);
  body.insertBefore(div_2, button);
}