Free Practice Quiz Question List

6 The DOM and Browser Events Online Quiz Questions

Use this free practice quiz with 20 questions to review 6 The DOM and Browser Events, test your knowledge, and prepare for your next test or exam.

20 questions
01
True or false
1 point

True or false: If you call document.querySelectorAll(), the returned collection is static, so it does not update automatically when the document later changes.

  1. A

    True

  2. B

    False

02
Written response
1 point

Which DOM method creates a new element that JavaScript can later insert into the document?

03
True or false
1 point

True or false: Adding defer to the example’s script lets the browser finish parsing the document before the script runs its element selectors.

  1. A

    True

  2. B

    False

04
Written response
1 point

An event listener needs to identify the object whose listener is currently running. Which event object property provides that information?

05
Choose one
1 point

A control should switch the CSS class selected on an element: add it when absent and remove it when present. Which expression does this?

  1. A

    element.className.toggle("selected")

  2. B

    element.classList.toggle("selected")

  3. C

    element.toggleClass("selected")

  4. D

    element.classList.replace("selected")

06
Choose one
1 point

A page variant may not contain an element matching #notice. What should code using document.querySelector("#notice") account for before trying to update the result?

  1. A

    It returns an empty static collection.

  2. B

    It creates a matching element automatically.

  3. C

    It returns null.

  4. D

    It throws an error whenever a selector has no match.

07
Choose one
1 point

A task form should add an item whether the user activates its submit button or submits from the keyboard. Which event should the handler listen for?

  1. A

    Listen only for clicks on the Add task button.

  2. B

    Listen for the form's submit event.

  3. C

    Listen only for key presses on the input.

  4. D

    Listen for changes to the task list.

08
True or false
1 point

True or false: When displaying user-supplied text, assigning it to textContent treats it as text rather than parsing it as HTML markup.

  1. A

    True

  2. B

    False

09
Choose all
1 point

A list has one click listener for its nested Remove buttons. Which statements correctly describe how this event-delegation approach can work? Select all that apply.

  1. A

    A click on a nested button can reach a listener on an ancestor list because clicks bubble.

  2. B

    A handler can use closest() to find a matching button related to the event target.

  3. C

    Every event bubbles, so delegation works identically for all event types.

  4. D

    A parent listener receives a descendant's click only if the descendant is removed first.

10
Fill in the blank
1 point

For an event that bubbles, it travels toward its target during the phase, runs target listeners at the target, and then travels back up through ancestors during the phase.

11
Fill in the blank
1 point

Complete each statement. For a cancelable event, event.preventDefault() but does not stop propagation. event.stopPropagation() but does not cancel the default browser action.

12
Choose all
1 point

In the example, what happens after the form handler receives a nonempty task? Select all that apply.

  1. A

    It creates a list item and appends it to the task list.

  2. B

    It resets the form after adding the task.

  3. C

    It allows the browser's normal form navigation to occur.

  4. D

    It focuses the task input after adding the task.

13
Open ended
1 point

In the task-list example, what happens when the user submits an input containing only whitespace? Explain why no task item is added.

14
Choose one
1 point

An application displays text supplied by a user. Which DOM update is recommended to show the value as text rather than parse it as markup?

  1. A

    Set the element's innerHTML to the supplied value.

  2. B

    Set the element's textContent to the supplied value.

  3. C

    Pass the supplied value to document.createElement().

  4. D

    Add the supplied value as a CSS class with classList.add().

15
Choose one
1 point

A script calls document.querySelectorAll(".notice") when no elements match. Later, it adds an element with class notice. What should the script expect from the collection returned by the original call?

  1. A

    The collection is static, so later matching elements are not added to it.

  2. B

    The collection is live, so it automatically includes later matching elements.

  3. C

    The result is null because the selector initially matches no elements.

  4. D

    The call returns only the first matching element.

16
Choose one
1 point

A task list receives new remove buttons as tasks are added. Which approach lets the list handle clicks on both existing and future buttons with one listener?

  1. A

    Attach a separate click listener to every button after each button is created.

  2. B

    Use querySelectorAll() once and assume its collection will include future buttons.

  3. C

    Attach one click listener to the list and identify clicked buttons in the handler.

  4. D

    Prevent the click event's default action so it reaches the list.

17
Choose one
1 point

A click on a button inside a list reaches a click listener attached to the list. In that listener, what do event.target and event.currentTarget identify?

  1. A

    target identifies where the event originated; currentTarget identifies the object whose listener is running.

  2. B

    target identifies the object whose listener is running; currentTarget identifies where the event originated.

  3. C

    Both properties always identify the object whose listener is running.

  4. D

    Both properties always identify the first ancestor that received the event.

18
Choose one
1 point

A form's submit handler calls event.preventDefault(). Which outcome is expected for a cancelable submit event?

  1. A

    It stops the event from reaching any ancestor but leaves the default action unchanged.

  2. B

    It cancels the event's default browser action but does not stop propagation.

  3. C

    It stops propagation and also cancels every browser default action.

  4. D

    It has no effect on a form's normal submission.

19
Written response
1 point

What JavaScript method registers a callback to run for a specified event type on an event target?

20
Written response
1 point

Which DOM property replaces an element's child content with text when assigned a new value?