MDN self-hosted library · e01.org

DOM scripting introduction

Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.

html
<!doctype html>
<html lang="en-US">
  <head>
    <meta charset="utf-8" />
    <title>Simple DOM example</title>
  </head>
  <body>
    <section>
      <img
        src="dinosaur.png"
        alt="A red Tyrannosaurus Rex: A two legged dinosaur
        standing upright like a human, with small arms, and a
        large head with lots of sharp teeth." />
      <p>
        Here we will add a link to the
        <a href="https://www.mozilla.org/">Mozilla homepage</a>
      </p>
    </section>
  </body>
</html>
js
   const link = document.querySelector("a");
js
   link.textContent = "Mozilla Developer Network";
js
   link.href = "https://developer.mozilla.org";
js
   const sect = document.querySelector("section");
js
   const para = document.createElement("p");
   para.textContent = "We hope you enjoyed the ride.";
js
   sect.appendChild(para);
js
   const text = document.createTextNode(
     " — the premier source for web development knowledge.",
   );
js
   const linkPara = document.querySelector("p");
   linkPara.appendChild(text);
js
sect.appendChild(linkPara);
js
sect.removeChild(linkPara);
js
linkPara.remove();
js
linkPara.parentNode.removeChild(linkPara);
js
   para.style.color = "white";
   para.style.backgroundColor = "black";
   para.style.padding = "10px";
   para.style.width = "250px";
   para.style.textAlign = "center";
html
   <p
     style="color: white; background-color: black; padding: 10px; width: 250px; text-align: center;">
     We hope you enjoyed the ride.
   </p>
html
   <style>
     .highlight {
       color: white;
       background-color: black;
       padding: 10px;
       width: 250px;
       text-align: center;
     }
   </style>
js
   para.classList.add("highlight");