javascriptintermediate
JavaScript DOM Manipulation
DOM manipulation and event handling
7 questions
By EZ4Code Team
1. What is the method to get an element by id?
document.getElementById('id')
document.getById('id')
document.querySelector('#id') only for class
document.find('id')
Explanation: getElementById gets a single element by its id; querySelector('#id') also works, but the former is more direct and efficient.
2. What does it mean when the third parameter (boolean) of addEventListener is true?
Triggers the event during the capture phase
Triggers the event during the bubbling phase
Triggers only once
Prevents default behavior
Explanation: A third parameter of true means triggering during the capture phase; false (default) means triggering during the bubbling phase.
3. What mechanism does event delegation mainly utilize?
Event bubbling, handling child element events uniformly on the parent element
Event capture
Event loop
Promise
Explanation: Event delegation leverages event bubbling to bind child element events uniformly to the parent element, reducing the number of bindings and supporting dynamic child elements.
4. What is the method to create a new element node?
document.createElement('div')
document.newElement('div')
document.create('div')
document.makeNode('div')
Explanation: createElement(tagName) creates an element node with the specified tag name, which needs to be inserted into the document with appendChild etc.
5. Which method can prevent the default behavior of an event?
event.preventDefault()
event.stopPropagation()
event.stop()
event.cancel()
Explanation: preventDefault() prevents default behavior (such as link navigation, form submission); stopPropagation() stops event bubbling.
6. What does the toggle method of classList do?
Toggles a class name: removes if present, adds if absent
Permanently adds a class name
Permanently removes a class name
Queries whether a class name exists
Explanation: classList.toggle(cls) toggles a class name on an element: if present, removes it and returns false; if absent, adds it and returns true.
7. Which property is used to set the text content of an element (avoiding XSS)?
textContent
innerHTML
outerHTML
innerText only for reading
Explanation: textContent sets/gets plain text without parsing HTML, avoiding XSS; innerHTML parses HTML strings, posing security risks.
More javascript Quizzes
JavaScript Basics Quiz
beginnerVariables, types, operators, functions, and core JavaScript concepts.
JavaScript ES6+ Quiz
intermediateArrow functions, destructuring, promises, modules, and modern JavaScript features.
JavaScript Asynchronous Programming
intermediatePromise, async/await, fetch
JavaScript Array Methods
intermediatemap, filter, reduce and other array higher-order methods
JavaScript Closures and Scope
advancedClosures, scope chain and hoisting