DOM Basics Flashcards

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?

A

??

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
Q

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

A
  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
Q

What is the textContent property of element objects?

A

how you access the text of an element, regardless of css rules

27
Q

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

A

element.textContent = ‘textstring’

28
Q

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

A

??

29
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

30
Q

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

A

so you don’t have to look it up every time

31
Q

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

A

no

32
Q

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

A

parent.appendChild(child);

33
Q

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

A

pass arguments as strings

ex/
element.setAttribute(‘class’, ‘class-name’)

or

(‘attribute-name’, ‘attribute-value’)

34
Q

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

A

Create Element
Add attributes (if any)
Add textNode / textContent (if any)
Append child

35
Q

What is the textContent property of an element object for?

A

To access or update text content of html element

textContent will create a textNode

36
Q

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

A

setAttribute

className

classList add
classList remove
classList toggle

37
Q

What are two advantages of defining a function to do create something

(like the work of creating a DOM tree)?

A

functions are repeatable and can be changed as needed easily

functionality is separate and can be switched up or changed as needed

38
Q

What is the event.target?

A

element that dispatched the event

point of interaction

39
Q

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

A

event bubbling allows you to listen for events on parent and still detect events in its children (it bubbles up)

40
Q

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

A

element.tagName

41
Q

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

A

argument: css selector(s)
returns: the element matching the selectors in argument –> assign to a $variable

(will return itself or closest ancestor)

42
Q

How can you remove an element from the DOM?

A

element.remove( );

43
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

put event listener on parent and use event delegation to listen to events on children

44
Q

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

A

it hides the element from view in the browser

it takes element out of normal flow

45
Q

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

A

argument: selectorString
return: boolean value

46
Q

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

A

element.getAttribute(‘attributeName’)

47
Q

At what steps of the solution would it be helpful to log things to the console?

A

at each step

ex/
after event is fired
after each if statement
after each for loop

48
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

document.createElement

49
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

manually change all views to hidden

then get click event target to view