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.

NoraW4·44 flashcards·44 vragen·1 weergaven
collegecomputer_scienceweb
0
Ken ik
1 / 44
0
Aan het leren
Voorkant

What does DOM stand for?

Tik om om te draaien
Achterkant

DOM stands for Document Object Model. It represents the structure of a web page as a tree of objects.

Tik om om te draaien
Ken ik
Aan het leren

Quiz(44 vragen)

Vraag 1 van 44

1. What does the term 'DOM' refer to in web development?

Termen in deze 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);

Vragen in deze set(44)

1. What does the term 'DOM' refer to in web development?

A.Document Object Model
B.Digital Online Media
C.Dynamic Object Management
D.Document Output Method

2. What is an event in JavaScript?

A.An action that occurs in the browser.
B.A method that manipulates DOM elements.
C.A function that calculates values.
D.A variable type in JavaScript.

3. What method would you use to retrieve an element by its class name?

A.getElementsByClassName
B.querySelector
C.getElementById
D.getElementsByName

4. True or False: The DOM allows for real-time updates to web pages.

A.True
B.False
C.Only for images
D.Only for text

5. Which method is used to attach an event listener to an element?

A.addEventListener
B.attachListener
C.bindEvent
D.listenForEvent

6. Which method allows you to remove an element from the DOM?

A.removeChild
B.deleteElement
C.detachElement
D.clearElement

7. Which of the following accurately describes a 'Node'?

A.Any object in the DOM tree, including elements, text, and comments
B.Only HTML elements
C.Only attributes of elements
D.Only text content

8. True or False: Events can only be triggered by keyboard actions.

A.True
B.False
C.Only on mobile devices
D.Only on form elements

9. Which property would you use to retrieve an element's current HTML content?

A.innerHTML
B.outerHTML
C.textContent
D.htmlContent

10. What is the purpose of the 'getElementById' method?

A.To retrieve an element by its unique ID
B.To create a new element
C.To remove an element
D.To modify the style of an element

11. What does the `stopPropagation` method do?

A.Prevents an event from bubbling up or capturing down.
B.Removes an event listener.
C.Stops all events on the page.
D.Prevents default actions only.

12. Fill in the blank: To replace an existing node, you can use _______.

A.replaceChild
B.swapNode
C.alterNode
D.changeChild

13. Fill in the blank: The structure of the DOM resembles a _______.

A.tree
B.grid
C.list
D.circle

14. What is the purpose of the `preventDefault` method?

A.To prevent the default action associated with an event.
B.To stop an event from propagating.
C.To remove an event listener.
D.To pause a JavaScript execution.

15. Which method would you use to listen for an event only once?

A.addEventListener
B.onceEventListener
C.addOnceListener
D.oneTimeListener

16. How would you select all elements of a certain class in the DOM?

A.querySelectorAll(className)
B.selectElementsByClass(className)
C.getElementsByClassName(className)
D.getAllElements(className)

17. Identify which of the following is NOT a mouse event.

A.click
B.mouseover
C.keydown
D.dblclick

18. Which of the following does NOT cause reflow in the DOM?

A.Changing the width of an element
B.Adding a new element
C.Changing an element's color
D.Removing an element

19. What does 'innerHTML' allow you to do?

A.Get or set the HTML content inside an element
B.Retrieve the style of an element
C.Clone an element
D.Count the number of child elements

20. What occurs during the capturing phase of event propagation?

A.The event travels from the root to the target element.
B.The event travels from the target to the root element.
C.No event listeners are triggered.
D.Only mouse events are handled.

21. What is the output of document.createElement('span').outerHTML?

A.<span></span>
B.<span>
C.<span/>
D.undefined

22. Cause → Effect: Using 'element.classList.add('newClass')'. What is the effect?

A.The element gains a new CSS class
B.The element is removed from the DOM
C.The element's content is cleared
D.The element is hidden

23. Which statement correctly describes event delegation?

A.Handling events at a higher level in the DOM.
B.Attaching an event listener to every element.
C.Using global event handlers.
D.Only handling mouse events.

24. When using insertBefore, which argument is NOT required?

A.newNode
B.referenceNode
C.parentNode
D.none

25. What is a NodeList?

A.A collection of DOM nodes
B.A type of CSS selector
C.A JavaScript function
D.A method for creating new elements

26. What does the `this` keyword refer to in a standard event handler function?

A.The element that triggered the event.
B.The global object.
C.The event itself.
D.The parent element.

27. Which statement about NodeList is true?

A.It can be transformed into an array
B.It is a live collection
C.It can use forEach method
D.It must be created manually

28. True or False: The DOM can only be modified after a page is loaded.

A.False
B.True
C.Only if using AJAX
D.Only in CSS

29. In the context of event handling, what is an event listener?

A.A function that executes when an event occurs.
B.A method that binds events to elements.
C.An object representing an event.
D.A variable storing event data.

30. What happens when you call cloneNode(true) on an element?

A.Clones the element without children
B.Clones the element with styles
C.Clones the element and all its descendants
D.Clones only the attributes

31. What does the 'createElement' method do?

A.Creates a new HTML element node
B.Retrieves an existing element
C.Removes an element from the DOM
D.Changes the style of an element

32. What is the bubbling phase in event propagation?

A.The event travels from the target to the root of the DOM.
B.The event travels from the root to the target.
C.The event does not occur.
D.Only keyboard events bubble.

33. Which of the following is an advantage of using event delegation?

A.Improves accessibility
B.Increases the number of event listeners
C.Reduces memory usage
D.Requires more JavaScript code

34. Comparison: What is the main difference between 'appendChild' and 'insertBefore'?

A.'appendChild' adds to the end while 'insertBefore' adds before a specified node
B.'appendChild' only works with text nodes
C.'insertBefore' removes a node
D.'appendChild' creates new nodes

35. Fill in the blank: An event listener can be added to a button using __________.

A.element.addEventListener('click', functionName);
B.button.setClickListener(functionName);
C.addClickListener(button, functionName);
D.button.onClick(functionName);

36. Which method is used to add a new attribute to an element?

A.setAttribute
B.addAttribute
C.createAttribute
D.defineAttribute

37. What does the 'document' object represent in the DOM?

A.The root of the DOM tree
B.A specific element in the DOM
C.The styling of the page
D.The JavaScript console

38. How do you remove an event listener from an element?

A.element.removeEventListener('click', functionName);
B.element.detachEvent('click', functionName);
C.element.deleteListener('click', functionName);
D.element.unbindEvent('click', functionName);

39. How would you remove a child element from its parent?

A.parentNode.removeChild(childNode)
B.removeElement(childNode)
C.deleteChild(parentNode)
D.parentNode.remove(childNode)

40. Which of the following is a property of the event object?

A.target
B.parent
C.listeners
D.typeOfEvent

41. Fill in the blank: The _______ method allows you to change the text content of an element.

A.textContent
B.innerHTML
C.setAttribute
D.appendChild

42. True or False: Arrow functions do not change the context of 'this' in event handlers.

A.True
B.False
C.Only in nested functions
D.Only in class methods

43. What does the 'setAttribute' method do?

A.Sets a specified attribute on an element
B.Retrieves the value of an attribute
C.Creates a new element
D.Removes an attribute from an element

44. What will happen if you call `event.preventDefault()` inside an event listener for a form submission?

A.The form will not be submitted and the page will not reload.
B.The form will be submitted immediately without any validation.
C.The event will not trigger any listeners attached to it.
D.The browser will display a confirmation dialog before submitting.

Gerelateerde sets

Maak je eigen studieset

Upload een PDF, plak je notities of beschrijf een onderwerp – AI genereert flashcards, quizzen en meer in seconden.