DOM Flashcards

1
Q

why do we log things to the console?

A

to examine if the browser is showing the right value

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

what is a “model”?

A

representation of the objects (replica)

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

web page that can be displayed in the browser window or as the html source.

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
  • data type object in javascript
  • DOM is javascript object which is a model of the html document
  • it is not your html document. it is a response for your html document
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

what is a DOM Tree?

A

The DOM represents a document with a logical tree. Each branch of the tree ends in a node, and each node contains objects.

parent elements containing child elements with nodes.

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 (only use this) - for consistency

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

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 the info that you need and access to it 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

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

to load the html and css first, and then javascript to be loaded

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

string of the css selector and returns the first one of the matching elements.

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 run?

A

css selector and returns a NodeList (collection of data that matches the element and can’t add items)

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? -> interactive web apps

A

Events are actions or occurrences that happen in the system you are programming, which the system tells you about so your code can react to them.

To react to an event, you attach an event handler to it. This is a block of code (usually a JavaScript function that you as a programmer create) that runs when the event fires.

When such a block of code is defined to run in response to an event, we say we are registering an event handler

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. can call a function without a parameter.

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

addEvenListener

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

what is a callback function?

A

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

17
Q

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

A

object with all data of whatever event occurred.

18
Q

what is the ‘event.target’?

A

points to the element that the event occurred.

19
Q
  • What is the difference between these two snippets of code?
    1) element.addEventListener(‘click’, handleClick)
    2) element.addEventListener(‘click’, handleClick())
A

the second one doesn’t access the object?
- uncaught typeerror: cannot read properties of undefined (reading ‘target’)

the parentheses are omitted where the function is called because they would indicate that the function should run as the page loads (rather than when the event fires)

20
Q

what is the ‘className’ property of element objects?

A

the className property of the Element interface gets and sets the value of the class attribute of the specified element.

21
Q

how do you update the css class attribute of an element using Javascript?

A

get the method from the object and add the className property

22
Q

what is the textContent property of element objects?

A

the textContent property of the Node interface represents the text content of the node and its descendants

23
Q

how do you update the text within an element using Javascript?

A

assigning to the textContent property.

24
Q

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

A

no. sometimes it’s not used inside of the function. but still put the ‘event’ parameter.
=> clear indicator that we’re using the eventHandler. never call that function itself.

25
Q

Would this assignment (dom-manipulation) be simpler or more complicated if we didn’t use a variable to keep track of the number of clicks?

A

It was less complicated with the variables
=> track the elements
=> javascript data stored in javascript..
=> DOM elements stored in dom…

26
Q

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

A

it’s easier for javascript to work with (put variables in javascript)

27
Q

does the ‘document.createElement()’ method insert a new element into the page?

A

it creates an element as a javascript object and just exists in your computer memory. (its not visible on the page)

28
Q

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

A

use appendChild()

29
Q

what do you pass the arguments to the ‘element.setAttribute()’ method?

A

string representing the attribute(property name) and the value inside of the html tag

30
Q

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

A

create => setAttribute or createTextNode => => query the element from the DOM => and append it using appendChild to the created element

31
Q

what is the ‘textContent’ property of an element object for?

A

The textContent property sets or returns the text content of the specified node, and all its descendants.

32
Q

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

A

setAttribute() or className.

33
Q

what are two advantages of defining a function to do create something (like creating a DOM)

A
  • easy to test the function

- reuse the function

34
Q

what is the ‘event.target’?

A

target property of the event which is referred when the event is fired.

35
Q

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

A

by using event delegation / event bubbling?

36
Q

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

A

target. tagName

37
Q

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

A

selector, This method travels up the DOM tree from the current element and returns the closest ancestor that matches the given parameter:

38
Q

How can you remove an element from the DOM?

A

remove method.

39
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

wrap the parent node in a variable and remove the child of the parent?