DOM Manipulation
Also DOM · Document Object Model
DOM manipulation is reading and changing the elements of a web page from JavaScript through the Document Object Model, a well-defined scheme for addressing page elements, typically by selecting elements with CSS selectors, reading or writing their content, and reacting to user events.
Know these firstHTML Structure
Formula#
Selecting elements#
document.querySelector("#id") // first element matching a CSS selector (#id = by id)
document.querySelector("button") // first <button>
document.querySelectorAll("p") // all <p> elements
document.getElementById("id") // by id, without "#"
Reading and writing#
let val = document.querySelector("#query").value; // content of an input field
document.querySelector("#query").value = "You win"; // set an input field
document.querySelector("#result").textContent = "Hello"; // text (safe against injection)
document.querySelector("#result").innerHTML = "<b>Bold</b>"; // parsed as HTML (unsafe)
Event handlers#
document.querySelector("button").onclick = function() { ... }; // click
document.querySelector("#query").onkeyup = function() { ... }; // after each keystroke
document.querySelector("#input").addEventListener("input", function() { ... }); // any change of the field
document.addEventListener("DOMContentLoaded", function() { ... }); // page parsed, DOM built
jQuery (deprecated)#
| jQuery | Vanilla JavaScript |
|---|---|
$("#query").val() / .val(x) | .value / .value = x |
$("#result").html(content) | .innerHTML = content |
$("#result").text(content) (safe) | .textContent = content |
$("button").click(function() {...}) | .onclick = function() {...} |
$("#query").keyup(function() {...}) | .onkeyup = … / addEventListener("keyup", …) |
$(document).ready(function() {...}) | addEventListener("DOMContentLoaded", …) |
$() is jQuery’s shorthand for element selection, with the same CSS selectors as querySelector.
Properties#
- Always prefer
.textContentover.innerHTMLfor untrusted content, to prevent code injection. onkeyupfires after each keystroke; theinputevent fires on every change of the field (also paste, undo), which suits live search (“search as you type”).- The DOM must exist before it is selected. A
<script>in<head>withoutdeferruns before<body>is parsed, soquerySelector("#input")returnsnull. Remedies:<script src="…" defer>, the<script>at the end of<body>, or wrapping the code in aDOMContentLoaded/$(document).readyhandler. - The selected element needs an
id(or another selectable feature) in the HTML; exam tasks therefore require “the input field and the paragraph should have an ID”. - The browser development console (F12), tab Elements, shows the current DOM; tab Console shows JavaScript output and errors.
Example#
Sources#
- Slides 10, p. 8 (DOM), p. 9 (
<script>,defer) - Slides 09, p. 14 (development console)
- DBIS_VL10 > DOM, DBIS_VL10 > JavaScript as part of the HTML, DBIS_VL09 > Browser development console
- DBIS Exam WS1617 (not published) T3.2, DBIS Exam WS1516 (not published) T3, DBIS Exam WS1920 (not published) T3.2, DBIS Exam WS2122 (not published) T4.2