DOM Basics Flashcards

(49 cards)

1
Q

What is the DOM?

A

document object model - a representation/standardization

can access and change all elements of HTML document

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

What does

document.querySelector()

return?

A

returns first element that matches selector

returns null if no matches

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

How do you modify the text of elements on the DOM?

A

element.textContent = ‘insert text content here’

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

What arguments does the addEventListener method take?

A
event to listen to
function to run when event occurs
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Give five examples of JavaScript events that can be listened for.

A
click
mousemove
dblclick
cut
copy 
paste
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does document.createElement() take as its argument?

A

the element being created

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

What does document.createElement() return?

A

DOM element

JS object representing a DOM element

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

How do you append elements to the DOM?

A

newElement.appendChild(newContent);

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

Why do we log things to the console?

A

so we can see what is going on with our code. checkpoints

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

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

A

so you can use it later

its faster because you don’t need to search the document for the items again

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
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
12
Q

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

A

takes any valid CSS selector and will return the element by name, class or ID

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

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

A

can take any valid CSS selector

returns nodeList

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

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

A

browser needs to parse / load to html – > DOM NEEDS TO LOAD

before js can access them

or else js will result in nulls

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

Is NodeList an array?

A

NodeList is NOT AN ARRAY
if you expand, a lot of the array methods are missing from list

are numerically indexed, have length property
thus you can loop through nodeLists

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

var name for DOM element

A

$heading

use a $

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

what is a callback function?

A
function that we give to another function
(we do not call it, we pass it to another function)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is the purpose of events and event handling?

A

to handle user input, user actions, and browser actions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
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
20
Q

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

A

callback function

21
Q

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

22
Q

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

A

target property of the event
can check with console.log(event.target)

will store the element interacted with

23
Q

What is the difference between these two snippets of code?

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

A

second one will return undefined

24
Q

What is the className property of element objects?

A

sets value of class attribute of specified element

25
How do you update the CSS class attribute of an element using JavaScript?
1. queryselect the DOM element (var $hotButton = document.querySelector('.hot-button') 2. use $variable.className = 'hot-button tepid'; (no dot notation for .className method) or $variable.className = 'hot-button' + temperature; (assuming var = temperature was declared and assigned)
26
What is the textContent property of element objects?
how you access the text of an element, regardless of css rules
27
How do you update the text within an element using JavaScript?
element.textContent = 'textstring'
28
Is the event parameter of an event listener callback always useful?
??
29
Would this assignment be simpler or more complicated if we didn't use a variable to keep track of the number of clicks?
more complicated
30
Why is storing information about a program in variables better than only storing it in the DOM?
so you don't have to look it up every time
31
Does the document.createElement() method insert a new element into the page?
no
32
How do you add an element as a child to another element?
parent.appendChild(child);
33
What do you pass as the arguments to the element.setAttribute() method?
pass arguments as strings ex/ element.setAttribute('class', 'class-name') or ('attribute-name', 'attribute-value')
34
What steps do you need to take in order to insert a new element into the page?
Create Element Add attributes (if any) Add textNode / textContent (if any) Append child
35
What is the textContent property of an element object for?
To access or update text content of html element textContent will create a textNode
36
Name two ways to set the class attribute of a DOM element.
setAttribute className classList add classList remove classList toggle
37
What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?
functions are repeatable and can be changed as needed easily functionality is separate and can be switched up or changed as needed
38
What is the event.target?
element that dispatched the event point of interaction
39
Why is it possible to listen for events on one element that actually happen its descendent elements?
event bubbling allows you to listen for events on parent and still detect events in its children (it bubbles up)
40
What DOM element property tells you what type of element it is?
element.tagName
41
What does the element.closest() method take as its argument and what does it return?
argument: css selector(s) returns: the element matching the selectors in argument --> assign to a $variable (will return itself or closest ancestor)
42
How can you remove an element from the DOM?
element.remove( );
43
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?
put event listener on parent and use event delegation to listen to events on children
44
What is the affect of setting an element to display: none?
it hides the element from view in the browser it takes element out of normal flow
45
What does the element.matches() method take as an argument and what does it return?
argument: selectorString return: boolean value
46
How can you retrieve the value of an element's attribute?
element.getAttribute('attributeName')
47
At what steps of the solution would it be helpful to log things to the console?
at each step ex/ after event is fired after each if statement after each for loop
48
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?
document.createElement
49
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?
manually change all views to hidden | then get click event target to view