Document Object Model Flashcards

1
Q

Why do we log things to the console?

A

to check our work

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

What is a “model”?

A

a recreation used to recreate the HTML page

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

Which “document” is being referred to in the phrase Document Object Model?

A

The document node, which includes the whole html itself

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

What is the word “object” referring to in the phrase Document Object Model?

A

it refers to each node being an object itself, which are JS objects

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

What is a DOM Tree?

A

a model of a webpage consisting of all of the elements and their nodes from parent elements downwards. A DOM tree can be the whole html page or just a small section of it.

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

Give two examples of document methods that retrieve a single element from the DOM.

A

queryselector () and getElementByID

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

Give one example of a document method that retrieves multiple elements from the DOM at once.

A

querySelectorAll()

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

Why might you want to assign the return value of a DOM query to a variable?

A

to refer to it and / or update it later using JavaScript

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

What console method allows you to inspect the properties of a DOM element object?

A

console.dir()

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

Why would a tag need to be placed at the bottom of the HTML content instead of at the top?

A

the browser needs to parse all the elements BEFORE the JS file can access them, so the JS file needs to go on the bottom

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

What does document.querySelector() take as its argument and what does it return?

A

it takes in a DOM string and returns an HTML object with the first class/id/name that it finds in your HTML with all its child contents

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

What does document.querySelectorAll() take as its argument and what does it return?

A

a DOM string and selectors to match and returns a NODE LIST containing an element object for each element that matches your search query

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

What is the purpose of events and event handling?

A

to respond in certain ways, or run certain codes/scripts when a user event happens, such as when they click/type/hover over something

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

What do [] square brackets mean in function and method syntax documentation?

A

it means they are optional and can be left out

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

What method of element objects lets you set up a function to be called when a specific type of event occurs?

A

.addEventListener()

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

What is a callback function?

A

a function that is passed in as an argument of another function, which will be ran later.

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

What object is passed into an event listener callback when the event fires?

A

the function object that was defined prior

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

What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?

A

it means the target that will trigger the event if it was acted on aka it is a reference to the object on which the event was dispatched. event.target gives you all the information about that target. Search MDN for more info if needed.

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

What is the difference between these two snippets of code?

element. addEventListener(‘click’, handleClick)
element. addEventListener(‘click’, handleClick())

A

In the first code, the handleClick function will not run until the button is clicked.

The second code is being called right away and handleClick() will run before the button is even clicked by the user.

20
Q

What event is fired when a user places their cursor in a form control?

A

focus

21
Q

What event is fired when a user’s cursor leaves a form control?

A

blur

22
Q

What event is fired as a user changes the value of a form control?

A

input

23
Q

What event is fired when a user clicks the “submit” button within a ?

A

submit

24
Q

What does the event.preventDefault() method do?

A

stops the browser from implementing its default submit method

25
Q

What does submitting a form without event.preventDefault() do?

A

the browser will proceed with its default submission methods, either post or get

26
Q

What property of a form element object contains all of the form’s controls?

A

elements property

27
Q

What property of form a control object gets and sets its value?

A

value

28
Q

What is one risk of writing a lot of code without checking to see if it works so far?

A

the missed errors will stack up, it would be way harder to debug

29
Q

What is an advantage of having your console open when writing a JavaScript program?

A

you can check your work step by step if needed!

30
Q

Does the document.createElement ( ) method insert a new element into the page?

A

No, it creates it internally

31
Q

How do you add an element as a child to another element?

A

AnotherElement.Appendchild ( elementToAdd)

32
Q

What do you pass as the arguments to the element.setAttribute( ) method?

A

it takes 2 arguments (‘name of your atr’, ‘value of atr’)

33
Q

What steps do you need to take in order to insert a new element into the page?

A

1) create
2) edit content/attribute if needed
3) append to where you want

34
Q

What is the textContent property of an element object for?

A

for us to view the text content of that element

35
Q

Name two ways to set the class attribute of a DOM element.

A

1) element.setAttribute ( )

2) element.setAttributeNode ( )

36
Q

What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?

A

1) the variables created during the function’s execution will be gone afterwards (save memory)
2) so YOU dont have to create it, saves time and prevent human errors

37
Q

What is the event.target?

A

element from where the event was fired

38
Q

What is the affect of setting an element to display: none?

A

browser will NOT render the element with that property at all.

39
Q

What does the element.matches() method take as an argument and what does it return?

A

it takes a string and returns a boolean

40
Q

How can you retrieve the value of an element’s attribute?

A

element.getAttribute

41
Q

If you were to add another tab and view to your HTML, but you didn’t use event delegation, how would your JavaScript code be written instead?

A

You would need to add an eventListener to each element required individually

42
Q

Why is it possible to listen for events on one element that actually happen its descendent elements?

A

by using dom delegation. you can set conditionals to see if the user clicks on any descendent elements

43
Q

What DOM element property tells you what type of element it is?

A

.tagName

44
Q

What does the element.closest() method take as its argument and what does it return?

A

it takes a CSS Selector string argument, and returns the element which is CLOSEST to your selected element

45
Q

How can you remove an element from the DOM?

A

element.remove ( )

46
Q

If you wanted to insert new clickable DOM elements into the page using JavaScript, how could you avoid adding an event listener to every new element individually?

A

by using dom delegation!