DOM Flashcards

1
Q

Why do we log things to the console?

A

To check functionality or accuracy of code

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

What is a “model”?

A

A representation of something

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 HTML page or document object

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

Each different part of the page

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

What is a DOM Tree?

A

A DOM tree, which is made of the document node, element nodes, attribute nodes and text nodes; or all the HTML elements that make up the page

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

getElementByID(), querySelector()

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

getElementsByClassName(), getElementsByTagName(), 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

If you need to work with an element more than once

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 script tag need to be placed at the bottom of the HTML content instead of at the top?

A

The HTML elements need to be processed before Javascript can access them

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

Uses a CSS selector and returns the first matching element

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

Uses a CSS selector to select all matching elements

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 listen and respond to user interactions

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

.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 being passed through another function as an argument

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 event object

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

What is the event.target?

A

A property of the event object that represents the HTML element that was interacted with and where the event is being dispatched.

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

Having () after a function name will call it regardless of whether there is an event and that returned result is being used in the addEventListener method

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

What is the className property of element objects?

A

A property of the Element interface that retrieves or sets the class of the specified element

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

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

A

element.className =

22
Q

What is the textContent property of element objects?

A

A property of the Element interface that retrieves or sets the text of the specified element

23
Q

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

A

element.textContent =

24
Q

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

A

Yes, to prevent any confusion with other potential events on the page; however, it is not required

25
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 complicated

26
Q

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

A

Storing allows us to reference the information again rather than rerunning the code to search through the document each time. In addition, the DOM is dynamic, whereas variables are static

27
Q

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

A

‘focus’

28
Q

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

A

‘blur’

29
Q

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

A

‘input’

30
Q

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

A

‘submit’

31
Q

What does the event.preventDefault() method do?

A

Prevents default actions from happening unless the event is triggered

32
Q

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

A

The form inputs are reset and the page refreshes

33
Q

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

A

.elements

34
Q

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

A

.elements.name.value

35
Q

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

A

No, you would have to use .appendChild() or .insertBefore()

36
Q

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

A

parent.appendChild(element)

37
Q

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

A

element.setAttribute(‘name’, ‘value’)

38
Q

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

A

Create the element, assign content to the element, add/append it to the DOM

39
Q

What is the textContent property of an element object for?

A

To retrieve or set text within an element

40
Q

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

A

.setAttribute(‘name’, ‘value’); .className = ‘value’; .classList.add(‘value’); .classList = ‘value’

41
Q

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

A

Having a reusable tool to input new data in a standard format
- Reusable, standardization

42
Q

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

A

screen and print

43
Q

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

A

<meta name=”viewport”>

44
Q

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

A

There is event bubbling that allows event listeners to begin from the element where the event took place and then go upwards out to the parent

45
Q

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

A

.tagName()

46
Q

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

A

The argument will represent the criteria of what you are searching for (i.e. class name, element type, etc). It returns the closest ancestor that matches the argument criteria

47
Q

How can you remove an element from the DOM?

A

element.remove()
No parameters

48
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

You can add an event listener to the parent and search for event targets with specified tag names

49
Q

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

A

Hides the element

50
Q

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

A

Takes in a selector and returns a boolean

51
Q

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

A

element.getAttribute()