DOM Flashcards

1
Q

Why do we log things to the console?

A

For debugging and/or making sure things work

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

What is a “model”?

A

Layout of the html document as a tree of objects

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

HTML

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

HTML elements as objects

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

What is a DOM Tree?

A

The HTML document in the form of a model of objects that looks like a tree

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

document.queryselector()
document.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

document.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

So you can access it later

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

If it was at the top, the website would stop loading until all of the javascript was downloaded

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

CSS selector as argument
Returns an element object representing the first element in the document that matches the selector

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

CSS selector as argument
Returns a NodeList containing one element object for each element that matches the selector

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

Handler is called by listener (I write handler)

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

Are all possible parameters required to use a JavaScript method or function?

A

No

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

Event listener

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

What is a callback function?

A
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

event

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

Returns the element that triggered the event

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

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

A

focus

20
Q

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

A

blur

21
Q

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

A

input

22
Q

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

A

submit

23
Q

What does the event.preventDefault() method do?

A

Prevents default behavior of the event

24
Q

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

A

refreshes the page

25
Q

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

A

form.elements

26
Q

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

A

value property

27
Q

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

A

Bugs/errors

28
Q

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

A

To check for bugs/errors

29
Q

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

A

It doesn’t until it is appended

30
Q

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

A

document.appendChild()

31
Q

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

A

Name of the attribute, value

32
Q

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

A

createElement -> append

33
Q

What is the textContent property of an element object for?

A

The text for the element

34
Q

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

A

classname or setAttribute or classList

35
Q

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

A

You can create it for multiple items at once, and since it’s a function it will be there for later use?

36
Q

What is the event.target?

A

It’s what element the user interacted with

37
Q

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

A

Bubbling

38
Q

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

A

event.target.tagName

39
Q

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

A

Css selector and returns the closest upward element or parent selected

40
Q

How can you remove an element from the DOM?

A

remove()

41
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

Adding an event listener to the parent element

42
Q

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

A

Removed from page

43
Q

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

A

make sure an element has a css selector

44
Q

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

A

.getAttribute()

45
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

Separate event handlers for each event listener

46
Q

If you didn’t use a loop to conditionally show or hide the views in the page, how would your JavaScript code be written instead?

A

Individual conditionals