Podcast
Questions and Answers
Which method retrieves an element by its id
attribute?
Which method retrieves an element by its id
attribute?
- document.querySelector()
- document.getElementById() (correct)
- document.querySelectorAll()
- document.getElementsByClassName()
The id
attribute of an HTML element must be unique within the entire document.
The id
attribute of an HTML element must be unique within the entire document.
True (A)
Which method returns all elements inside a specified element that matches a CSS selector?
Which method returns all elements inside a specified element that matches a CSS selector?
querySelectorAll
The method elem.__________
returns the first element that matches a CSS selector.
The method elem.__________
returns the first element that matches a CSS selector.
Match the DOM searching methods with their descriptions:
Match the DOM searching methods with their descriptions:
Which of the following methods can only be called on the document
object?
Which of the following methods can only be called on the document
object?
The matches()
method searches the DOM to find elements that match a CSS selector.
The matches()
method searches the DOM to find elements that match a CSS selector.
Which method is used to find the nearest ancestor (including the element itself) that matches a CSS selector?
Which method is used to find the nearest ancestor (including the element itself) that matches a CSS selector?
The __________
method returns true
if one element is a descendant of another.
The __________
method returns true
if one element is a descendant of another.
Which of the following is a live HTMLCollection?
Which of the following is a live HTMLCollection?
The querySelectorAll()
method returns a live collection of elements.
The querySelectorAll()
method returns a live collection of elements.
What character can be used as the tag
parameter in the getElementsByTagName()
method to select all elements, regardless of their tag?
What character can be used as the tag
parameter in the getElementsByTagName()
method to select all elements, regardless of their tag?
The method document.__________
returns elements with the specified name attribute.
The method document.__________
returns elements with the specified name attribute.
In the context of DOM searching, what is the term for an element's parent, the parent of its parent, and so on, up to the top of the document?
In the context of DOM searching, what is the term for an element's parent, the parent of its parent, and so on, up to the top of the document?
The elem.closest(css)
method includes the element itself in the search for the nearest matching ancestor.
The elem.closest(css)
method includes the element itself in the search for the nearest matching ancestor.
What is the primary difference between querySelector()
and querySelectorAll()
?
What is the primary difference between querySelector()
and querySelectorAll()
?
Which DOM searching method is considered the most versatile due to its ability to use any CSS selector?
Which DOM searching method is considered the most versatile due to its ability to use any CSS selector?
If there are multiple elements with the same id
in a document, the behavior of document.getElementById()
is __________.
If there are multiple elements with the same id
in a document, the behavior of document.getElementById()
is __________.
Which of the following methods returns a static collection of elements?
Which of the following methods returns a static collection of elements?
If a JavaScript variable is declared with the same name as an element's id
, the variable will always take precedence over the DOM element reference.
If a JavaScript variable is declared with the same name as an element's id
, the variable will always take precedence over the DOM element reference.
What does the following code output?
First div
Second div
let divs = document.querySelectorAll('div');
alert(divs.length);
// A new div is added to the DOM here (not shown in code)
alert(divs.length);
What does the following code output?
First div
Second div
let divs = document.querySelectorAll('div');
alert(divs.length);
// A new div is added to the DOM here (not shown in code)
alert(divs.length);
What is the return type of document.getElementsByTagName('div')
?
What is the return type of document.getElementsByTagName('div')
?
The __________
property of an element can be modified to change its visual appearance using CSS.
The __________
property of an element can be modified to change its visual appearance using CSS.
Which method can be used to determine if an element matches a specific CSS pseudo-class?
Which method can be used to determine if an element matches a specific CSS pseudo-class?
The use of id-named global variables to access elements is a recommended practice for modern JavaScript development.
The use of id-named global variables to access elements is a recommended practice for modern JavaScript development.
Given the following HTML:
A
What does the following JavaScript code output?
```javascript
let list = document.querySelector('ul');
let result = list.closest('div');
console.log(result);
Given the following HTML:
A
What does the following JavaScript code output?
```javascript
let list = document.querySelector('ul');
let result = list.closest('div');
console.log(result);
Explain the difference between a live HTMLCollection and a static NodeList and provide an example of a method that returns each.
Explain the difference between a live HTMLCollection and a static NodeList and provide an example of a method that returns each.
The __________
method is useful when iterating over a set of elements and needing to filter elements based on whether they match a certain CSS selector.
The __________
method is useful when iterating over a set of elements and needing to filter elements based on whether they match a certain CSS selector.
What is the potential issue with the following code?
document.getElementsByTagName('input').value = 5;
What is the potential issue with the following code?
document.getElementsByTagName('input').value = 5;
The document.contains()
method can be used to check if a specified element is the root element of the document.
The document.contains()
method can be used to check if a specified element is the root element of the document.
Match the following methods with their correct behavior when no matching element is found:
Match the following methods with their correct behavior when no matching element is found:
Given the following HTML:
Item 1
What will document.querySelector('div > li')
return?
Given the following HTML:
Item 1
What will document.querySelector('div > li')
return?
Why is it generally better to use document.querySelector()
or document.querySelectorAll()
instead of the older document.getElementsBy*()
methods?
Why is it generally better to use document.querySelector()
or document.querySelectorAll()
instead of the older document.getElementsBy*()
methods?
Assuming the following HTML structure:
Click me
The JavaScript code document.querySelector('button').__________('div.container')
can be used to find the closest ancestor with the class 'container'
Assuming the following HTML structure:
Click me
The JavaScript code document.querySelector('button').__________('div.container')
can be used to find the closest ancestor with the class 'container'
What is a key advantage of using elem.matches(css)
within a loop when processing multiple DOM elements?
What is a key advantage of using elem.matches(css)
within a loop when processing multiple DOM elements?
If elemA.contains(elemB)
returns true
, then elemB.contains(elemA)
will always return false
unless elemA
and elemB
are the same element.
If elemA.contains(elemB)
returns true
, then elemB.contains(elemA)
will always return false
unless elemA
and elemB
are the same element.
Given the following code, what will be the output?
let outer = document.getElementById('outer');
let inner = document.getElementById('inner');
console.log(outer.contains(inner));
console.log(inner.contains(outer));
Given the following code, what will be the output?
let outer = document.getElementById('outer');
let inner = document.getElementById('inner');
console.log(outer.contains(inner));
console.log(inner.contains(outer));
To retrieve all elements with the class 'highlight' within a specific element (e.g., with ID 'content'), you can use the following JavaScript code: document.getElementById('content').__________('.highlight');
To retrieve all elements with the class 'highlight' within a specific element (e.g., with ID 'content'), you can use the following JavaScript code: document.getElementById('content').__________('.highlight');
Consider this code snippet:
First
Second
const divs = document.getElementsByTagName('div');
divs[0].append('Third')
console.log(divs.length)
What is the value of divs.length
?
Consider this code snippet:
First
Second
const divs = document.getElementsByTagName('div');
divs[0].append('Third')
console.log(divs.length)
What is the value of divs.length
?
Given there are elements with name='my-element'
, then document.getElementsByName('my-element')
excludes elements created dynamically after the initial page load.
Given there are elements with name='my-element'
, then document.getElementsByName('my-element')
excludes elements created dynamically after the initial page load.
How would you retrieve every <a>
tag with an href
whose value ends in .pdf
inside a div
with id='container'
, and then print the href
of the 3rd such element?
How would you retrieve every <a>
tag with an href
whose value ends in .pdf
inside a div
with id='container'
, and then print the href
of the 3rd such element?
Given an element elem
, the code elem.__________(elem)
would return true
.
Given an element elem
, the code elem.__________(elem)
would return true
.
Flashcards
document.getElementById(id)
document.getElementById(id)
A method to get an element by its id
attribute.
elem.querySelectorAll(css)
elem.querySelectorAll(css)
Returns a collection of elements inside elem
matching the CSS selector.
elem.querySelector(css)
elem.querySelector(css)
Returns the first element inside elem
matching the specified CSS selector.
elem.matches(css)
elem.matches(css)
Signup and view all the flashcards
elem.closest(css)
elem.closest(css)
Signup and view all the flashcards
elem.getElementsByTagName(tag)
elem.getElementsByTagName(tag)
Signup and view all the flashcards
elem.getElementsByClassName(className)
elem.getElementsByClassName(className)
Signup and view all the flashcards
document.getElementsByName(name)
document.getElementsByName(name)
Signup and view all the flashcards
Live Collection
Live Collection
Signup and view all the flashcards
Static Collection
Static Collection
Signup and view all the flashcards
elemA.contains(elemB)
elemA.contains(elemB)
Signup and view all the flashcards
Study Notes
- DOM navigation properties are helpful when elements are nearby, but searching methods become necessary for locating arbitrary elements on a page.
getElementById
document.getElementById(id)
retrieves an element by itsid
attribute, regardless of its location in the document.- A global variable named after the
id
of an element references that element, unless a JavaScript variable with the same name is declared. - Usage of
id
-named global variables to access elements is discouraged due to potential naming conflicts and reduced code clarity. document.getElementById
is the preferred method in real-world scenarios.- The
id
attribute must be unique within the document. getElementById
can only be called on thedocument
object. It searches the entire document for the specifiedid
.
querySelectorAll
elem.querySelectorAll(css)
returns all elements insideelem
that match the provided CSS selector.- CSS pseudo-classes like
:hover
and:active
are supported within the CSS selector.
querySelector
elem.querySelector(css)
returns the first element that matches the given CSS selector.- It's faster and more efficient than
querySelectorAll
when only one element is needed.
matches
elem.matches(css)
checks ifelem
matches the given CSS selector and returnstrue
orfalse
.- Useful for filtering elements within a collection based on CSS selectors.
closest
elem.closest(css)
searches for the nearest ancestor (including the element itself) that matches the CSS selector.- It traverses up the DOM tree from the element, checking each parent until a match is found.
getElementsByTagName
elem.getElementsByTagName(tag)
finds elements with the given tag name and returns a collection of them.- The
tag
parameter can be "*" to select all tags.
getElementsByClassName
elem.getElementsByClassName(className)
returns elements that have the specified CSS class.
getElementsByName
document.getElementsByName(name)
returns elements with the givenname
attribute document-wide, and is rarely used.
Common Mistakes
- Forgetting the "s" in getElementsByTagName.
- Attempting to directly assign a value to a collection of elements instead of iterating or accessing elements by index.
Live vs. Static Collections
getElementsBy*
methods return a live collection, which reflects the current state of the document and auto-updates when changes occur.querySelectorAll
returns a static collection, which is a fixed array of elements.
Summary of Searching Methods
querySelector
: Searches by CSS selector, can be called on an element, returns a static collection.querySelectorAll
: Searches by CSS selector, can be called on an element, returns a static collection.getElementById
: Searches by ID, can only be called on the document, returns a single element.getElementsByName
: Searches by thename
attribute, can only be called on the document, returns a live collection.getElementsByTagName
: Searches by tag name, can be called on an element, returns a live collection.getElementsByClassName
: Searches by class name, can be called on an element, returns a live collection.
Additional Methods
elem.matches(css)
: Checks if an element matches a CSS selector.elem.closest(css)
: Finds the nearest ancestor that matches a CSS selector.elemA.contains(elemB)
: Checks ifelemB
is insideelemA
(a descendant) or if they are the same element.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.