Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
Loading…
Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
Loading…
Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
JAVASCRIPT DOM / CHAPTER 04 / Learn / LESSON 02
querySelector(selector) returns the first matching element or null. Selectors look like CSS: #id, .class, tag.
Store the result in a variable, then read or write properties. If the element is missing, access throws — check carefully in later lessons.
Mini example:
const title = document.querySelector("#title"); title.textContent = "Budget";
Common mistake: querySelectorAll (a list) instead of querySelector; or a selector without # for an id.
<h1 id="title">Draft</h1><p id="sub">summary</p>The #title selector finds the element by id.
Press Run — HTML loads in a sandbox iframe; Stop removes the iframe (also kills infinite loops).
›