Week #6: Tutorials + 13 DOM Manipulation + Blog Question.

Tutorials:

Blog Question:

Describe how the querySelector() and querySelectorAll() methods work. What is the difference between the values returned for each method? Use an example code snippet to support your answer; include sample HTML and JS selectors.

Both of these methods return either a single element or collection of elements, just in different ways. The querySelector()method returns the first single element in the document that matches the CSS-type selector. The querySelectorAll()method returns a collection of all elements in the document that matches the selector.

querySelector()

<body>
    <h2>DOM example or whatever lol</h2>
    <h2>This is a second h2</h2>
</body>

let tag = document.querySelector("h2");
//this returns the first h2: DOM example or whatever lol

querySelectorAll()

<body>
    <h2>DOM example or whatever lol</h2>
    <h2>This is a second h2</h2>
</body>

let tag = document.querySelectorAll("h2");
//this returns all of the h2 tags

Leave a comment

Your email address will not be published. Required fields are marked *

css.php