Searching the DOM
Browser Document: Searching the DOM
What does searching the DOM mean?
View Answer:
// getElementById: This method gets an element by its id attribute:
let element = document.getElementById("myId");
// getElementsByClassName: This method gets all elements with a specified class name:
let elements = document.getElementsByClassName("myClass");
// querySelector: This method returns the first element that matches a specified CSS selector (id, class selectors, types, attributes, relationships, etc.):
let element = document.querySelector(".myClass");
What is the purpose of getElementById() in JavaScript?
View Answer:
How do we gain access to an element by its id attribute?
View Answer:
<!-- Get Element by getElementById -->
<div id="elem">
<div id="elem-content">Element</div>
</div>
<script>
// get the element
let elem = document.getElementById('elem');
// make its background red
elem.style.background = 'red';
</script>
<!-- Get it by just ID -->
<div id="elem">
<div id="elem-content">Element</div>
</div>
<script>
// elem is a reference to DOM-element with id="elem"
elem.style.background = 'red';
// id="elem-content" has a hyphen inside, so it can't be a variable name
// ...but we can access it using square brackets: window['elem-content']
</script>
What is the main rule for naming an element id attribute?
View Answer:
Can you call getElementById on any object/element we choose?
View Answer:
Can you explain the function of the querySelectorAll() method?
View Answer:
Syntax: elementList = parentNode.querySelectorAll(selectors);
<ul>
<li>The</li>
<li>test</li>
</ul>
<ul>
<li>has</li>
<li>passed</li>
</ul>
<script>
let elements = document.querySelectorAll('ul > li:last-child');
for (let elem of elements) {
console.log(elem.innerHTML); // "test", "passed"
}
</script>
Can you use pseudo-classes with querySelectorAll()?
View Answer:
We use the :checked
pseudo-class with querySelectorAll()
:
let checkedInputs = document.querySelectorAll('input:checked');
checkedInputs.forEach(input => {
console.log(input.value);
});
This script selects all checked input
elements and logs their values.
Can you explain the function of the querySelector() method?
View Answer:
Syntax: elementList = parentNode.querySelector(selectors);
<!DOCTYPE html>
<html>
<body>
<p class="myClass">Hello, world!</p>
<button onclick="changeText()">Click me</button>
<script>
function changeText() {
var paragraph = document.querySelector(".myClass");
paragraph.textContent = "Hello, JavaScript!";
}
</script>
</body>
</html>
In this example, when the button is clicked, the changeText()
function is called. This function uses querySelector()
to find the first paragraph with the class "myClass", and then changes its text content to "Hello, JavaScript!".
The argument to querySelector()
is a string containing one or more CSS selectors separated by commas. In this case, .myClass
is a class selector that selects elements with the class myClass
. You could also use other types of selectors, like #myId
for an ID selector or div
for a type selector.
How does querySelectorAll() differ from querySelector()?
View Answer:
Here's an example of how to use querySelectorAll()
:
<!DOCTYPE html>
<html>
<body>
<p class="myClass">Hello, world!</p>
<p class="myClass">Hello again, world!</p>
<button onclick="changeText()">Click me</button>
<script>
function changeText() {
var paragraphs = document.querySelectorAll(".myClass");
paragraphs.forEach(function(paragraph) {
paragraph.textContent = "Hello, JavaScript!";
});
}
</script>
</body>
</html>
In this example, when the button is clicked, the changeText()
function is called. This function uses querySelectorAll()
to find all paragraphs with the class "myClass", and then changes their text content to "Hello, JavaScript!".
Please note that querySelectorAll()
returns a NodeList. Although a NodeList is not an Array, it can be used in a similar way in many cases. In particular, you can use the forEach()
method to iterate over all the nodes in the list. However, some Array methods, like push()
or pop()
, are not available on a NodeList.
How does the matches() method work?
View Answer:
Syntax: let result = element.matches(selectorString);
<a href="http://example.com/file.zip">...</a>
<a href="http://ya.ru">...</a>
<script>
// can be any collection instead of document.body.children
for (let elem of document.body.children) {
if (elem.matches('a[href$="zip"]')) {
console.log('The archive reference: ' + elem.href);
}
}
</script>
Can you explain the function of the Element.closest() method?
View Answer:
Syntax: let closestElement = targetElement.closest(selectors);
<h1>Contents</h1>
<div class="contents">
<ul class="book">
<li class="chapter">Chapter 1</li>
<li class="chapter">Chapter 1</li>
</ul>
</div>
<script>
let chapter = document.querySelector('.chapter'); // LI
console.log(chapter.closest('.book')); // UL
console.log(chapter.closest('.contents')); // DIV
console.log(chapter.closest('h1')); // null (because h1 is not an ancestor)
</script>
How does the getElementsByTagName() method function?
View Answer:
Syntax: elements = element.getElementsByTagName(tagName);
<table id="table">
<tr>
<td>Your age:</td>
<td>
<label>
<input type="radio" name="age" value="young" checked /> less than 18
</label>
<label>
<input type="radio" name="age" value="mature" /> from 18 to 50
</label>
<label>
<input type="radio" name="age" value="senior" /> more than 60
</label>
</td>
</tr>
</table>
<script>
let inputs = table.getElementsByTagName('input');
for (let input of inputs) {
console.log(input.value + ': ' + input.checked); // console.logs young: true
}
</script>
Does getElementsByTagName() return an element or a collection?
View Answer:
// doesn't work
document.getElementsByTagName('input').value = 5;
// should work (if there's an input)
document.getElementsByTagName('input')[0].value = 5;
Novice coders mistake the getElementsByTagName return for an element, which is incorrect. That fails because it takes a collection of inputs and assigns the value rather than the elements inside it. We should either loop over the collection or retrieve an element by index and assign it.
What is the difference between a live and static collection?
View Answer:
<!-- LIVE COLLECTION -->
<div>First div</div>
<script>
let divs = document.getElementsByTagName('div');
console.log(divs.length); // 1
</script>
<div>Second div</div>
<script>
console.log(divs.length); // 2
</script>
<!-- STATIC COLLECTION -->
<div>First div</div>
<script>
let divs = document.querySelectorAll('div');
console.log(divs.length); // 1
</script>
<div>Second div</div>
<script>
console.log(divs.length); // 1
</script>
What is the difference between a NodeList and an HTMLCollection?
View Answer:
Let's look at examples of using a NodeList and an HTMLCollection.
1. NodeList example using querySelectorAll()
, which returns a static NodeList:
let nodeList = document.querySelectorAll("p");
console.log(nodeList[0]); // Access the first <p> element
// This will include all <p> elements, even if new ones are added to the document
2. HTMLCollection example using getElementsByClassName()
, which returns a live HTMLCollection:
let htmlCollection = document.getElementsByClassName("myClass");
console.log(htmlCollection[0]); // Access the first element with class="myClass"
// This will always reflect the current set of elements with class="myClass", even if they change
In both cases, we can access individual elements using array-like index syntax (e.g., nodeList[0]
or htmlCollection[0]
).
Remember that "live" means the collection automatically updates when the document changes. So if you add a new element with class="myClass", it will automatically appear in htmlCollection
. However, nodeList
from querySelectorAll()
is "static" and will not update to reflect changes to the document.
What are the advantages of using querySelector() over getElementById()?
View Answer:
<!DOCTYPE html>
<html>
<body>
<p id="myId" class="myClass">Hello, world!</p>
<p class="myClass">Hello again, world!</p>
<button onclick="changeText()">Click me</button>
<script>
function changeText() {
// Using querySelector to select by class and change text of the FIRST matching element
var paragraph = document.querySelector(".myClass");
paragraph.textContent = "Hello, OpenAI!";
}
</script>
</body>
</html>
In this example, when the button is clicked, the changeText()
function is called. This function uses querySelector()
to find the first paragraph with the class "myClass", and then changes its text content to "Hello, OpenAI!".
This demonstrates one advantage of querySelector()
over getElementById()
: the ability to select elements based on their class, not just their ID. Other advantages include the ability to use more complex selectors, like attribute selectors, pseudo-class selectors, and combinations of multiple selectors.
Can you use querySelector() to search within a specific element?
View Answer:
<!DOCTYPE html>
<html>
<body>
<div id="myDiv">
<p class="myClass">Hello, world!</p>
</div>
<p class="myClass">Hello again, world!</p>
<button onclick="changeText()">Click me</button>
<script>
function changeText() {
var myDiv = document.getElementById("myDiv");
// Using querySelector to search within 'myDiv'
var paragraph = myDiv.querySelector(".myClass");
paragraph.textContent = "Hello, OpenAI!";
}
</script>
</body>
</html>
In this example, when the button is clicked, the changeText()
function is called. This function first selects the <div>
element with the ID "myDiv" using getElementById()
. Then, it uses querySelector()
on this specific <div>
element to find the first <p>
element within the <div>
that has the class "myClass". It changes the text content of this <p>
element to "Hello, OpenAI!".
The second <p>
element with the class "myClass" (outside the <div>
) is not affected, because querySelector()
is called on myDiv
, not on the entire document. This demonstrates how you can use querySelector()
to search within a specific element.
How can you search the DOM for an element with a specific attribute value?
View Answer:
<!DOCTYPE html>
<html>
<body>
<button data-function="save">Save</button>
<button data-function="load">Load</button>
<script>
var saveButton = document.querySelector('[data-function="save"]');
console.log(saveButton);
// Outputs: <button data-function="save">Save</button>
</script>
</body>
</html>
In this example, querySelector()
is used with the attribute selector [data-function="save"]
to find the first element with a data-function
attribute of "save". The resulting element is then logged to the console.
You can replace "save"
with any attribute value you're looking for, and data-function
with any attribute name. Note that the attribute name and value are case-sensitive.
The querySelectorAll()
method can be used in the same way to find all elements that match the selector, not just the first match.
Can you chain multiple querySelector() calls together?
View Answer:
let childElement = document.querySelector('.parent-class').querySelector('.child-class');
In this example, the script first selects the element with the class parent-class
. Then, within that element, it selects the element with the class child-class
.
What is the difference between querySelector and getElementById in terms of performance?
View Answer:
Can you combine multiple selectors in a single querySelector call?
View Answer:
// Example using querySelector
let firstLiInUl = document.querySelector('ul li:first-child');
// selects the first li element that is a child of a ul element.
///////////////////////////////
// Example using querySelectorAlL
let elems = document.querySelectorAll('.sandwich, #contact label');
// Get's any element with the .sandwich class, and all labels inside the #contact element
How can you find all elements of a specific tag within a specific element?
View Answer:
let divElement = document.querySelector('div');
let pElementsInDiv = divElement.querySelectorAll('p');
pElementsInDiv.forEach(p => {
console.log(p.textContent);
});
In this example, the script first selects a div
element. Then, within that div
, it selects all p
elements and logs their text content.