DOM Flashcards

1
Q

Dom Querying
Why do we log things to the console?

A

to make sure your script is loading properly
to check for bugs, make sure everything is working smoothly

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

Dom Querying
What is a ‘model’?

A

something recreating something else

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

Dom Querying
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

Dom Querying
What is the word ‘object’ referring to in the phrase Document Object Model?

A

it’s referring to different parts of the page loaded in the browser window

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

Dom Querying
What is a DOM Tree?

A

an element and all of its configurations

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

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

A

getElementById(‘id’)
querySelector(‘css selector’) (querySelector is the only one you have to use)

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

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

A

querySelectorAll(‘css selector’)

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

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

A

make it easier to use, otherwise you have to write it out every time

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

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

A

dir()

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

Dom Querying
Why would a

 tag need to be placed at the bottom of the HTML content instead of at the top?
A

because the browser needs to parse all the elements in the HTML page before Javascript code can access them

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

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

A

a string containing one or more css selectors
returns: first element that matches the specified selector or group of selectors

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

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

A

a string containing one or more selectors
returns a static (not live) nodelist containing one element object for each element that matches at least one of the specified selectors or an empty nodelist (not an array) if there isn’t a match

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

DOM Events:
Why do we log things to the console?

A

so that we know if we have bugs

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

DOM Events:
What is the purpose of events and event handling?

A

so that users can interact with the web page

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

DOM Events:
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
16
Q

DOM Events:
What method of element objects let 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
17
Q

DOM Events:
what is a callback function?

A

it is a function that is passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action

18
Q

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

A

an object that has data to

19
Q

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

A

the element where the event occurred
you can console.log if you’re not sure
mdn for more info

20
Q

DOM Events:
what is the difference between these two snippets of code?
element.addEventListener(‘click’, handleClick)
element.addEventListener(‘click’, handleClick())

A

the first is being passed is a callback function
second gets called immediately and doesn’t get to be an event handler, gets replaced by its return

21
Q

DOM Manipulation:
What is the className property of element objects?

A

it gets and sets the value of a specific class attribute of a specific element
element.className = ‘attribute’

22
Q

DOM Manipulation:
how do you update the CSS class attribute of an element using JavaScript?

A

element.className(‘class’, element(getAttribute(‘class’))

23
Q

DOM Manipulation:
What is the textContent property of element objects?

A

allows you to update text content

24
Q

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

A

textContent

25
Q

DOM Manipulation:
Is the event parameter of an event listener callback always useful?

A

yes..?

26
Q

DOM Manipulation:
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

27
Q

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

A
28
Q

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

A

no

29
Q

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

A

elementYouWantToAppendTo.appendChild(elementYouWantAppended)

30
Q

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

A

name of attribute
value assigned to the attribute

31
Q

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

A
  1. create the element, need to assign to variable
    var newEl = document.createElement(‘element’)
  2. give it content, assign to variable
    var name = document.createTextNode(‘text content’)
  3. add it to the DOM.
    var position = document.getElementByTagName(‘element’)[0]
    position.appendChild(newEl)
32
Q

DOM Creation:
What is the textContent property of an element object for?

A

it represents the text content of the nodes and its descendants or assign new textContent

33
Q

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

A

Element.setAttribute(name, value)
class name

34
Q

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

A

you can use it again in the future
you don’t have to type everything out again when you’re using the same function

35
Q

DOM Event Delegation:
What is the event.target?

A

it is the element the event interacted with

36
Q

DOM Event Delegation:
Why is is possible to listen for events on one element that actually happen on its descendent elements?

A

because of bubbling

37
Q

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

A

tagName

38
Q

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

A

it takes selectors
and returns the closest ancestor (parent) element, itself, or null
(opposite of querySelector)

39
Q

DOM Event Delegation:
How can you remove an element from the DOM?

A

element.remove()
no arguments and returns

40
Q

DOM Event Delegation:
if you wanted to insert new clickable DOM elements into the page using JavaScript, how could you avoid adding an even listener to every new element individually?

A

add it to the parent element