DOM Quiz - Languages JavaScript
Free practice quiz: DOM (Languages JavaScript). 47 multiple-choice questions with answers and explanations. Play online, no sign-up needed.
Play all 47 questions freeSample questions
Which method is used to select an HTML element by its ID in the DOM?
- getElementsByClassName()
- querySelector()
- getElementById()
- getElementsByTagName()
Show answer
Answer: C. getElementById()
The `getElementById()` method is specifically designed to retrieve a single element from the DOM based on its unique `id` attribute.
What is a 'node' in the context of the DOM?
- Only an HTML element
- Any part of an HTML document, including elements, text, attributes, and comments
- A JavaScript function
- A CSS rule
Show answer
Answer: B. Any part of an HTML document, including elements, text, attributes, and comments
The DOM represents an HTML document as a tree structure where each component (element, text, attribute, etc.) is considered a node.
What is an HTML `data-*` attribute used for?
- To define styling for an element.
- To store custom data private to the page or application.
- To set the element's `id`.
- To specify the element's tag name.
Show answer
Answer: B. To store custom data private to the page or application.
`data-*` attributes allow you to embed custom data attributes on all HTML elements, which can then be used by JavaScript or CSS.
How do you get the value of a specific attribute, like 'href', from an element?
- element.get('href')
- element.attributes('href')
- element.getAttribute('href')
- element.property('href')
Show answer
Answer: C. element.getAttribute('href')
The `getAttribute()` method is used to retrieve the value of a specified attribute.
Which method is used to remove a specific attribute from an HTML element?
- removeAttribute()
- deleteAttribute()
- remove(attributeName)
- setAttribute(attributeName, null)
Show answer
Answer: A. removeAttribute()
The `removeAttribute(name)` method removes the specified attribute from the element.