DOM Flashcards

1
Q

Why do we log things to the console?

A

to debug things and figure it all out

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

What is a “model”?

A

something recreating or ‘modeling’ something else
a tree of the stuff in the html

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 document

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

refers to the data type object in js

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

What is a DOM Tree?

A

a model of a webpage
-an element + all its configuration + all its contents, represented as an object in the dom

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
-the only method you ever need to use to retrieve 1 thing from dom is 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

document.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

store it incase you need to use it more. Otherwise, you’ll have to query something 100x
storing as variables > queryselectoring x 100, because queryselectoring searches the whole page a lot

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

document.dir() method

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

Why would a

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

because you need to scan through the whole page to find stuff
your js would load before your html that it references

if you queryselector something that doesn’t exist, u get null
script at bottom is important

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

it takes a css selector, it returns the element and its stuff

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

takes a css selector, returns a nodelist containing all the elements

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

Nodes? Nodelists?

A

nodelists -> a collection of nodes. Not arrays, just a collection

Nodes -> The DOM Node interface is an abstract base class upon which many other DOM API objects are based, thus letting those object types to be used similarly and often interchangeably. As an abstract class, there is no such thing as a plain Node object. All objects that implement Node functionality are based on one of its subclasses. Most notable are Document, Element, and DocumentFragment.

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

What is the purpose of events and event handling?

A

it makes the page intractable

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

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

A

addEventListener(‘eventtypehere’, function)

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

What is a callback function?

A

when you pass a function as an argument to another function

yo dawg i heard u like functions

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

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

A

an object with a bunch of data to describe the event that occured… the “event object”

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

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

A

its the element where the event originated from.

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

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

A

2nd one calls it right that second and will run it before the click ever happens and it never ends up being an event handler

1st one callbacks a function

21
Q

What is the className property of element objects?

A

it gets and sets the current classes applied to the element

literally the value of the class attribute

it’s NOT a css selector

22
Q

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

A

element.classname = ‘agjkafkg’

23
Q

What is the textContent property of element objects?

A

allows you to get or set the text of the element

24
Q

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

A

element.textcontent = “jakldjfklasdjflk”

25
Q

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

A

no, but MOST of the time it will

26
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

complicated

27
Q

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

A
28
Q

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

A

focus

29
Q

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

A

blur

30
Q

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

A

input

31
Q

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

A

submit

dont put submit event handler on submit button… needs to be on the form itself

NEVER EVER have a click event listener on submit button

32
Q

What does the event.preventDefault() method do?

A

prevents default behavior of the event

need it for forms because of the way forms were originally programmed. Things have changed

33
Q

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

A

it prevents the page from automatically reloading with the form’s values in the URL

ALWAYS CALL THIS SHIT

34
Q

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

A

form.elements,

35
Q

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

A

value property

36
Q

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

A

if it doesn’t work from like line 2 and you’re on 50 lines, you won’t know what works and what doesnt

37
Q

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

A
38
Q

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

A

no

39
Q

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

A

parent.appendchild(child)

40
Q

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

A

setAttribute(‘nameofvalueyouwannachange’, ‘whatyouwannachangeitto’)

41
Q

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

A

query where you wanna put it first…. unless you already did that

create the element, give it content(textnode), appendchild

42
Q

What is the textContent property of an element object for?

A

add text OR see text of a current element

43
Q

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

A

className, or setAttribute

also, classList, which has a set of methods for stuff

classname classlist setattribute are basically personal choice.
changing 1 class value? classlist probably good
changing the whole class thingy? setattribute prob good

44
Q

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

A
45
Q

what is the affect of display.none?

A

it makes it disappear

46
Q

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

A

takes css selector, returns boolean (for whether or not the selector matches)

47
Q

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

A

getattributes() - use the name of the attribute u want

48
Q

if you were to add another tab and view to your html, but didnt use event delegation, how would your js code be written instead

A

need conditionals for each possible view

49
Q

if you didnt use a loop to conditionally show or hide the views in the page, how would your js code be written instead

A