DOM Flashcards

1
Q

What is a “model”?

A

A template to follow

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

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

A

the current html document

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

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

A

The elements of the document are represented as objects

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

What is a DOM tree?

A

A visual representation of all of the document’s nodes and their relation to one another

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

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

A

document. querySelector(‘css selector’)

document. getElementById(‘id’)

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

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

A

document. querySelectorAll( )
document. getElementsByClassName( )
document. getElementsByTagName( )

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

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

A

The variable does not hold the value of the node itself, but a reference to where that node is stored. So, if you want to do multiple things to that node, it is useful to store it in a variable so that the computer does not have to search for it every time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
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
9
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 through the entire HTML body

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

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

A

It takes in a CSS selector and returns the first element that matches the result

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

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

A

CSS selector as argument

Returns a NodeList of all elements that match

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

What is the purpose of events and event handling?

A

Makes the page more interactive by triggering scripts when certain events happen

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

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

A

.addEventListener(event, callback function)

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

What is a callback function?

A

A function that is passed as an argument into another function.

A function that you don’t want to call right away so you store for later

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

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

A

the event object

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

What is the event.target? If you’re not sure, where would you find information about it?

A

property that returns the object which the event was bounded onto

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

Difference between:

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

A

The first one is a callback function; Only runs when the event occurs

Second one is a normal function call and it runs as the web page is loaded only.

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

What is the className property of element objects?

A

returns a string of the value of the class attribute for the element

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

How do you update the CSS class attribute of an element using JavaScript?

A

update the className property of the element

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

What is the textContent property of element objects?

A

Gets the text content of the element as well as the text content of its children(?)

21
Q

How do you update the text within an element using JavaScript?

A

with the .textContent property of an element

22
Q

Is the event parameter of an event listener callback always useful?

A

No, but always present. Do not forget event parameter when defining event handler functions

23
Q

Would this assignment be simpler or more complicated if we didn’t use a variable to keep track of the number of clicks?

A

More difficult as the click counter allows us to control both the text content and the button color

24
Q

Why is storing information about a program in variables better than only storing it in the DOM?

A

It is easier to reference especially if using multiple times

25
Q

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

A

focus event

26
Q

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

A

blur event

27
Q

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

A

‘input’ event

28
Q

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

A

‘submit’ event

29
Q

What does the event.preventDefault( ) do?

A

tells browser that even if an event does not get explicitly handled, its default action should not be taken

30
Q

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

A

The page will refresh with the user’s inputs added to the URL

31
Q

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

A

form.elements

array-like object(?) that you can access via indexes

32
Q

What property of a form control object lets you get and set its value?

A

.value

33
Q

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

A

Will inevitably run into bugs/errors and it will be much harder to debug if there’s a lot of code to go through

34
Q

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

A

Can see if any errors are occurring and confirm the program is running as expected.

35
Q

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

A

No; need the appendChild( ) method

36
Q

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

A

parent-element.appendChild(aChild)

.append( )

37
Q

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

A

setAttribute(attribute, value)

38
Q

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

A

Create a new element and assign it to a variable
Use the variable to assign textContent to it
append the new element onto a parent with appendChild

39
Q

What is the textContent property of an element object for?

A

Gets or sets the text node of an element

40
Q

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

A

element. className
element. setAttribute( )
element. classList.remove( )

41
Q

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

A

Helps break down the work you need into chunks as well as allow you to re-use/repeat the function as many times as needed

42
Q

Give two examples of media features that you can query in an @media rule?

A

width

orientation

43
Q

Which HTML meta tag is used in mobile-responsive web design?

A

meta name=”viewport” content=”width=device-width, initial-scale=1.0”

44
Q

What is the event.target?

A

the element that the user actually interacted with, but as a result of event bubbling, it caused an event to get triggered

45
Q

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

A

Event bubbling

46
Q

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

A

element.tagName

47
Q

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

A

takes in a css selector as argument and returns the closest ancestor that matches

48
Q

How can you remove an element from the DOM?

A

element.remove( )

49
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 adding the event listener to a parent and then relying on event bubbling and conditions to find the actual element you want