Document Object Model Flashcards

1
Q

Why do we log things to the console?

A

to trouble shoot

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

What is a “model”?

A

a replica of the original

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 whole 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

a collection of a whole bunch of objects from 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 web page made of objects

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

document.queryselector() and getelementbyid

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

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

A

because you need to save the elements in variables in order to use dir on them. Or, you have to pass the call to querySelector to the dir method. And because once we select that object we have it saved already in that variable, so if we need to acces it again we can just use the variable instead of having to use the dom again

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

because you need to save the elements in variables in order to use dir on them. Or, you have to pass the call to querySelector to the dir method. And because once we select that object we have it saved already in that variable, so if we need to acces it again we can just use the variable instead of having to call the dom element again

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

A

because the html elements needs to load first

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

a css selector as a string or the element selector as its string and it returns the first one it encounters, or null if theres nothing

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

a css selector as a string or element and it returns all the elements that match

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 for things like clicks, hover etc

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,make sure their available as needed, but dont have to use

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 that is passed into 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

event object

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

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

A

it is the element thats was interacted with and mdn

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

first one the second argument is the function and in the second one is going to call the argument automatically and it will ignore the first argument

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

it represent the class attribute of the 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

using the classname property or classlist since it only adds or removes specific classes

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

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

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

A

its faster when you need to re use it

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

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

A

text content property

25
Q

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

A

not always sometimes you don to have to use it

26
Q

what is hoisting

A

JavaScript Hoisting refers to the process whereby the interpreter appears to move the declaration of functions, variables or classes to the top of their scope, prior to execution of the code. Hoisting allows functions to be safely used in code before they are declared.

27
Q

what is the purpose of .target

A

The target property of the Event interface is a reference to the object onto which the event was dispatched.

28
Q

where will children appear in the dom (incase you want to overlay something)

A

children will always be on top of their parent elments and siblings will be next to each other

29
Q

do the event target listener functions always need and if statement
EXAMPLE:
$orangeButton.addEventListener(‘click’, normal);

A
no as long as you specify what happens inside once the event is runned it should work
EXAMPLE:
function normal(event) {
  $popUp.classList.add('hidden');
  $popUp.classList.remove('black');

}

30
Q

what is event bubbling

A

Event bubbling is a type of event propagation where the event first triggers on the innermost target element, and then successively triggers on the ancestors of the target element in the same nesting hierarchy till it reaches the outermost DOM element or document object.

31
Q

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

A

focus

32
Q

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

A

blur

33
Q

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

A

input

34
Q

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

A

submit event

35
Q

What does the event.preventDefault() method do?

A

cancels the event meaning that any default action normally taken by the implementation as a result of the event will not occur.

prevents page from reloading

36
Q

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

A

refreshes the page

37
Q

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

A

element
using
document.forms

38
Q

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

A

no it only creates it

39
Q

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

A

with appendchild() method

40
Q

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

A

name and value

41
Q

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

A

create element, give it content, and then appendchild

42
Q

What is the textContent property of an element object for?

A

it adds text content to it

43
Q

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

A

setAttribute, classname, classlist

44
Q

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

A

its reusable

45
Q

What is the event.target?

A

the element in where the event is being fired upon

46
Q

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

A

event bubbling

47
Q

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

A

.tagname
Example:
event.target.tagname

48
Q

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

A

it takes a selector and returns the closest element with that tag

49
Q

How can you remove an element from the DOM?

A

remove() method

50
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 a listener on the parent

51
Q

what is event delegation

A

Event Delegation is basically a pattern to handle events efficiently. Instead of adding an event listener to each and every similar element, we can add an event listener to a parent

52
Q

What is the event.target?

A

the element where the user interacted with

53
Q

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

A

it removes it from the document flow and hides it

54
Q

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

A

a selector string returns a boolean telling you if it matches the selector

55
Q

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

A

get attribute method

56
Q

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

A

all the time

57
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

an event listener on each of the elements and a bunch more functions

58
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

make alot of conditional statements