It’s now time to tie together our HTML, CSS, and JavaScript knowledge by using the Document Object Model (DOM) to manipulate a webpage. Most of the real-time interactions you see on a webpage are made possible by the DOM. Let’s make some magic!
By the end of this lesson, you'll be able to:
`) is treated as an object. Because these elements are objects, we can use special commands (called methods and properties) to change them, remove them, or add new elements. - **Model:** The model is how all these objects are arranged in a structured, tree-like format called the **DOM tree**. You might hear the terms “node,” “object,” and “element” — all of these are used to describe parts of the DOM. ---
#
.
` element in the document. | | `document.querySelector('.magic')` | Selects the **first** element with the class `"magic"`. | | `document.querySelector('#hero')` | Selects the **first** element with the ID `"hero"`. | | `document.querySelector('.magic p')` | Selects the **first** `
` inside an element with the class `"magic"`. | The method `document.querySelectorAll()` works similarly, but instead of just one element, it will select **all** elements that match the query. ---
document.querySelector(complete);
querySelector('.complete');
document.querySelector = '.complete';
document.querySelector('.complete');
elements. ``` > Note: When using `removeAttribute()`, if you're working with multiple elements (like all `
` tags), you’ll need to loop through them to remove the attribute from each one. ---
Only 30 days left in our monthly sale. Move fast!
Your passwords did not match.
` element and assign it to the `paragraph` variable. Right now, the paragraph exists in JavaScript but has no content yet. 3. Use the `innerText` property to add the message: `"Your passwords did not match."` 4. Use `setAttribute()` to give the paragraph a class of `registration-error`. Notice that we’re using `innerText` and `setAttribute()` on the `paragraph` variable. In JavaScript, this variable represents the new element, and everything you add to it will eventually appear in the HTML when it’s added to the page. ---