Any2Quiz
Computer Science / Languages / JavaScript

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 free

Sample questions

  1. Which method is used to select an HTML element by its ID in the DOM?

    1. getElementsByClassName()
    2. querySelector()
    3. getElementById()
    4. 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.

  2. What is a 'node' in the context of the DOM?

    1. Only an HTML element
    2. Any part of an HTML document, including elements, text, attributes, and comments
    3. A JavaScript function
    4. 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.

  3. What is an HTML `data-*` attribute used for?

    1. To define styling for an element.
    2. To store custom data private to the page or application.
    3. To set the element's `id`.
    4. 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.

  4. How do you get the value of a specific attribute, like 'href', from an element?

    1. element.get('href')
    2. element.attributes('href')
    3. element.getAttribute('href')
    4. element.property('href')
    Show answer

    Answer: C. element.getAttribute('href')

    The `getAttribute()` method is used to retrieve the value of a specified attribute.

  5. Which method is used to remove a specific attribute from an HTML element?

    1. removeAttribute()
    2. deleteAttribute()
    3. remove(attributeName)
    4. setAttribute(attributeName, null)
    Show answer

    Answer: A. removeAttribute()

    The `removeAttribute(name)` method removes the specified attribute from the element.

Take the full quiz

More quizzes

Browse all JavaScript quizzes