JS Course 1: DOM Manipulation and Events Flashcards

1
Q

What is the DOM?

A

DOM stands for Document Object Model and is a tree like representation of the contents of a web page. There are “nodes” in the tree with different relationships depending on how they’re arranged in the document.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are nodes?

A

A node is a single point in the node tree. It can have a parent and/or child. When working with the DOM, you can use selectors to select the nodes!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are query selectors?

A

Query selectors are a reference to a part of the DOM based off a selector like a CSS selector name, id selector, a relationship to an element based off nesting, parenting, etc., based off what type of element it is. etc.

This does not create an element, but refers to an element that already exists in the HTML document.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are DOM Methods?

A

DOM methods are methods that change or alter the DOM. These methods don’t actually change the HTML, but only change how the HTML is interacted with and changes based off certain situations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the keyword “defer”? What does it do?

A

This keyword is used in the

 tag when put in the head of the HTML document to instruct the browser to run scripts AFTER the HTML document is processed. This prevents confusion and unexpected results when scripts are ran before the document is processed.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is an event?

A

An event (in this context) refers to the ways a user can interact with a page and what a page does based off the event. Event’s can be a click. double click, hover, a certain key pressed down, etc.

When used in functions concerning events, e is the object used to access the event itself! This can be used in many different, useful ways.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the “keyCode” property? What does it do?

A

This property is based off a data-key (a number that is assigned to a keyboard input to determine what keyboard event/key is pressed.)

Don’t use this! Instead use the KeyboardEvent: code property

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the KeyboardEvent: code property?

A

The KeyboardEvent: code property is used more modernly instead of keyCode. Use this! Not keyCode!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the “this” keyword?

A

This keyword refers to an object. This keyword can be used in many different ways and what it returns depends entirely on the situation and object it refers to.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the “once” option?

A

This option when set true listens for a click and then unbinds itself. Basically, the item cannot be activated again after an event when clicked once (until you refresh the page)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you target the nodes you want to work with?

A

Node selectors like id’s, classes, etc. Style selectors plus style selectors that can show the relationship between elements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you create an element in the DOM?

A

Using the syntax…
const variable = document.createElement(tagName)

where the tagName is the type of element you want to create like an h1, p, div, etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you add an element to the DOM?

A

You append it using the syntax parentNode.child

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you remove an element from the DOM?

A

Same syntax as adding an element to the DOM, but with removeChild:

parentNode.removeChild(child)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How can you alter an element in the DOM?

A

You can add styles to an element in the DOM through inline or class styles. You can also add text with

createdNode.textContent = “text”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

When adding text to a DOM element, should you use textContent or innerHTML? Why?

A

textContent since it has less room for security risks than innerHTML has.

17
Q

Where should you include your JavaScript tag in your HTML file when working with DOM nodes?

A

You can add them at the end of the <body> element if you want to do inline JS, or you can put them in the <head> still, but you must add the “defer” keyword at the end of the opening tag.

18
Q

How do “events” and “listeners” work?

A

Events and listeners work by watching what specific events (pressing a certain key, clicking, double clicking. etc.) and based off those events do a certain type of actions.

19
Q

What are three ways to use events in your code?

A

1) You can add an event to a button where if you press the button an image appears.

2) You can have a tab on a page that has a text-button where if you press that text-button a tab opens to show you information.

3) You can press enter if you’re on a bulleted line in Notion to created a new bulleted line (like this one!)

20
Q

Why are event listeners the preferred way to handle events?

A

They allow you to apply several lines of code (things to be done) for one event.

21
Q

What are the benefits of using named functions in your listeners?

A

It decreases the amount of code you need to use, improves readability, etc.

22
Q

How do you attach listeners to groups of nodes?

A

Using querySelectorAll to select a list of elements.

23
Q

What is the difference between the return values ofquerySelectorandquerySelectorAll?

A

“querySelectorAll” returns an “array-like-list” that acts like an array but not quite. querySelector just allows one match.

24
Q

What does a “nodelist” contain?

A

A list of all the items matching a specific selector. A collection of document nodes (element nodes, attribute nodes, and text nodes)

25
Q

Explain the difference between “capture” and “bubbling.”

A

Capture describes the browser going top-down on a web page to figure out where the an event happens, and bubbling is the opposite going bottom-up.