JavaScript MCQs – DOM Manipulation and Events

11.) How do you add a new attribute to an element in JavaScript?

A) element.setAttribute(name, value)
B) element.addAttribute(name, value)
C) element.createAttribute(name, value)
D) element.attribute(name, value)

Answer: Option A

Explanation: The setAttribute() method adds a new attribute or changes the value of an existing attribute on an element.

12.) What does the target property of an event object return?

A) The type of the event.
B) The element that triggered the event.
C) The parent element of the event.
D) The default action of the event.

Answer: Option B

Explanation: The target property refers to the element that triggered the event.

13.) How can you dynamically create a new DOM element in JavaScript?

A) document.newElement()
B) document.addElement()
C) document.createElement()
D) document.generateElement()

Answer: Option C

Explanation: The createElement() method is used to dynamically create a new DOM element.

14.) What is event bubbling in JavaScript?

A) Events triggering in reverse order from the target element to the root.
B) Events triggering from the target element up to its ancestors.
C) Events triggering only on the target element.
D) Events that do not propagate.

Answer: Option B

Explanation: Event bubbling occurs when an event propagates from the target element to its parent and up the DOM tree.

15.) What is the default phase of event propagation?

A) Bubbling phase
B) Capturing phase
C) Target phase
D) None of the above

Answer: Option A

Explanation: By default, events propagate in the bubbling phase unless explicitly specified.

16.) How do you stop event propagation in JavaScript?

A) event.preventPropagation()
B) event.stopPropagation()
C) event.preventDefault()
D) event.clear()

Answer: Option B

Explanation: The stopPropagation() method stops the event from propagating further in the DOM hierarchy.

17.) Which method adds an event listener to an element?

A) addListener()
B) registerEvent()
C) attachListener()
D) addEventListener()

Answer: Option D

Explanation: The addEventListener() method is used to add event listeners to elements.

18.) How do you manually trigger a click event on an element in JavaScript?

A) element.invoke(“click”)
B) element.callEvent(“click”)
C) element.click()
D) element.trigger(“click”)

Answer: Option C

Explanation: The element.click() method programmatically triggers the click event on the specified element.

19.) How can you check if an element has a specific class?

A) element.classList.contains(className)
B) element.isClass(className)
C) element.hasClass(className)
D) element.containsClass(className)

Answer: Option A

Explanation: The classList.contains() method checks if an element has a specific class.

20.) How do you toggle a class on an element?

A) element.classToggle(className)
B) element.toggle(className)
C) element.classList.toggle(className)
D) element.switchClass(className)

Answer: Option C

Explanation: The classList.toggle() method toggles a class on or off for an element.

Leave a Reply

Your email address will not be published. Required fields are marked *