JavaScript DOM and events notes
This study set covers essential concepts of the JavaScript DOM and events, providing clear explanations for key terms and functionality. It is designed for college students seeking to understand how to manipulate web pages dynamically and handle user interactions.
Quiz(44 questions)
1. What does the term 'DOM' refer to in web development?
Terms in this Study Set(44)
JavaScript DOM Basics(16)
What does DOM stand for?
DOM stands for Document Object Model. It represents the structure of a web page as a tree of objects.
True or False: The DOM is a programming interface.
True: The DOM is a programming interface for web documents that allows scripts to update content, structure, and styles dynamically.
Element vs. Node: What's the difference?
Element: Represents an HTML tag. Node: Any object in the DOM tree, including elements, text, and comments.
What does 'getElementById' do?
'getElementById' retrieves an element by its unique ID, allowing manipulation of that specific element.
Fill in the blank: The DOM is structured like a _______.
tree: The DOM structure is tree-like, with a root node and child nodes.
How to select multiple elements?
Use 'querySelectorAll(selector)'. It returns a NodeList of all matching elements for the given selector.
What is 'innerHTML'?
'innerHTML' is a property that gets or sets the HTML content inside an element, allowing for dynamic updates.
Cause → Effect: Adding a class to an element.
Cause: Use 'element.classList.add('className')'. Effect: The element gains the specified CSS class, potentially changing its appearance.
What is a NodeList?
A NodeList is a collection of nodes returned by methods such as 'querySelectorAll', which can be looped through to access each element.
True or False: The DOM is static.
False: The DOM is dynamic, meaning it can be modified by scripts, reflecting changes immediately in the web page.
What does 'createElement' do?
'createElement' creates a new HTML element node in memory, which can then be added to the DOM.
Comparison: 'appendChild' vs. 'insertBefore'.
'appendChild': Adds a node as the last child. 'insertBefore': Inserts a node before a specified existing node.
What is 'document' in the DOM?
'document' is the root of the DOM tree and provides access to all elements, methods, and properties of the web page.
How to remove an element?
Use 'parentNode.removeChild(childNode)'. This method removes a specified child node from its parent.
Fill in the blank: The _______ method changes the text content of an element.
'textContent': This method sets or gets the text content of an element, ignoring HTML tags.
What does 'setAttribute' do?
'setAttribute' sets a specified attribute on an element, allowing for dynamic updates of attributes like 'src' or 'class'.
Event Handling in JavaScript(16)
What is an event in JavaScript?
An event is an action or occurrence that happens in the browser, such as a mouse click, key press, or page load.
Define event listener.
An event listener is a function that waits for a specific event to occur on a particular element and executes when the event occurs.
True or False: Events can only be triggered by mouse actions.
False. Events can be triggered by user interactions like keyboard input, touch events, or page lifecycle events.
How do you attach an event listener?
Use the `addEventListener` method on a DOM element. For example: element.addEventListener('click', functionName);
What is event propagation?
Event propagation is the process of how an event travels through the DOM. It consists of two phases: capturing and bubbling.
Fill in the blank: The event __________ phase occurs first in event propagation.
capturing
Compare capturing phase and bubbling phase.
- Capturing: Event travels from root to target. - Bubbling: Event travels from target to root.
What method removes an event listener?
Use `removeEventListener`. For example: element.removeEventListener('click', functionName);
Event object properties include:
- `target`: Element that triggered the event. - `type`: Type of the event (e.g., 'click').
What does event delegation mean?
Event delegation is a technique to handle events at a higher level in the DOM rather than on individual elements, improving performance.
True or False: Preventing default behavior is possible with an event.
True. Use `event.preventDefault()` to prevent default actions like form submissions or link navigations.
Cause → Effect: Click on a button → ?
The button triggers a defined event listener function.
How can you stop event propagation?
Use `event.stopPropagation()` to prevent the event from bubbling up or capturing down the DOM.
What are mouse events?
Mouse events react to mouse actions, including 'click', 'dblclick', 'mouseover', and 'mouseout'.
Short example of an event listener.
const button = document.querySelector('button'); button.addEventListener('click', () => alert('Button clicked!'));
What does the 'this' keyword refer to in event handlers?
In event handlers, 'this' refers to the element that fired the event unless arrow functions are used.
Advanced DOM Manipulation(12)
What is DOM traversal?
The process of navigating through the DOM tree to access or manipulate elements. Methods include getElementById, querySelector, and parentNode.
true or false: innerHTML alters element structure.
True. Using innerHTML can change an element's content and structure, which may also remove event listeners.
Comparing appendChild vs. insertBefore.
appendChild adds a node as the last child. insertBefore places a node before a specified child.
How to clone a node?
Use the cloneNode method. Example: let clone = originalNode.cloneNode(true); // true for deep clone.
What is event delegation?
A technique that involves attaching a single event listener to a parent element instead of multiple listeners on child elements, enhancing performance.
Fill in the blank: use _______ to set multiple CSS styles.
style.cssText; It allows setting multiple CSS styles in a single string format.
What does NodeList represent?
NodeList is an array-like collection of nodes returned by methods like querySelectorAll. Unlike arrays, it lacks methods like map or forEach.
Cause → Effect: Using removeChild.
Cause: Call removeChild on a parent element. Effect: The specified child element is removed from the DOM.
What is the purpose of DocumentFragment?
A lightweight container for DOM manipulation. It allows batch operations without reflow, improving performance.
Create a new element in JavaScript.
Use document.createElement('div'); This creates a new div element, which can then be styled and appended.
True or False: setAttribute only changes attributes.
False. setAttribute can also add new attributes if they do not exist.
How to listen for multiple events?
Use addEventListener for each event type. Example: element.addEventListener('click', handler); element.addEventListener('dblclick', handler);
Questions in this Study Set(44)
1. What does the term 'DOM' refer to in web development?
2. What is an event in JavaScript?
3. What method would you use to retrieve an element by its class name?
4. True or False: The DOM allows for real-time updates to web pages.
5. Which method is used to attach an event listener to an element?
6. Which method allows you to remove an element from the DOM?
7. Which of the following accurately describes a 'Node'?
8. True or False: Events can only be triggered by keyboard actions.
9. Which property would you use to retrieve an element's current HTML content?
10. What is the purpose of the 'getElementById' method?
11. What does the `stopPropagation` method do?
12. Fill in the blank: To replace an existing node, you can use _______.
13. Fill in the blank: The structure of the DOM resembles a _______.
14. What is the purpose of the `preventDefault` method?
15. Which method would you use to listen for an event only once?
16. How would you select all elements of a certain class in the DOM?
17. Identify which of the following is NOT a mouse event.
18. Which of the following does NOT cause reflow in the DOM?
19. What does 'innerHTML' allow you to do?
20. What occurs during the capturing phase of event propagation?
21. What is the output of document.createElement('span').outerHTML?
22. Cause → Effect: Using 'element.classList.add('newClass')'. What is the effect?
23. Which statement correctly describes event delegation?
24. When using insertBefore, which argument is NOT required?
25. What is a NodeList?
26. What does the `this` keyword refer to in a standard event handler function?
27. Which statement about NodeList is true?
28. True or False: The DOM can only be modified after a page is loaded.
29. In the context of event handling, what is an event listener?
30. What happens when you call cloneNode(true) on an element?
31. What does the 'createElement' method do?
32. What is the bubbling phase in event propagation?
33. Which of the following is an advantage of using event delegation?
34. Comparison: What is the main difference between 'appendChild' and 'insertBefore'?
35. Fill in the blank: An event listener can be added to a button using __________.
36. Which method is used to add a new attribute to an element?
37. What does the 'document' object represent in the DOM?
38. How do you remove an event listener from an element?
39. How would you remove a child element from its parent?
40. Which of the following is a property of the event object?
41. Fill in the blank: The _______ method allows you to change the text content of an element.
42. True or False: Arrow functions do not change the context of 'this' in event handlers.
43. What does the 'setAttribute' method do?
44. What will happen if you call `event.preventDefault()` inside an event listener for a form submission?
Related Study Sets
Wiederholung: HTML CSS JavaScript Rollen
Wiederholung: Browser lädt eine Seite
Test: Was eine URL ist
What a URL is
HTML vs CSS vs JavaScript step by step
How a browser loads a page
REST APIs and HTTP methods exam review
HTML CSS en JavaScript stap voor stap
Create Your Own Study Set
Upload a PDF, paste your notes, or describe a topic – AI generates flashcards, quizzes and more in seconds.

