DOM Flashcards

1
Q

What is a “model”?

A

a representation of the HTML Page that includes all html elements, attributes, and text

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

Which “document” is being referred to in the phrase Document Object Model?

A

the HTML Page

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

What is the word “object” referring to in the phrase Document Object Model?

A

html Elements, attributes, text.

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

What is a DOM Tree?

A

It is a model of a webpage that shows all HTML elements, attributes, and text. It looks similar to a family tree.

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

Give two examples of document methods that retrieve a single element from the DOM.

A

1) getElementById

2) use a CSS Selector

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

Give one example of a document method that retrieves multiple elements from the DOM at once.

A

1) getElementByClassName()
2) getElementsByTagName()
3) querySelector

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

When you need to work with an element more than once. This is called. ‘caching’

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

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

A

it returns the first Element within the parameters or group of selectors.

document.querySelector(‘selectors’)

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

It is load order, HTML needs to load first, then the javascript so we can access all the Elements and attributes.

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

Why do we log things to the console?

A

for debugging and for values

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

What is the purpose of events and event handling?

A

It makes the page interactive.
An event means, that something has happend on the page, it can be either triggered by mouse, keyboards, or the UI itself.

An event handling is the process that responds to the even. “Hey this event happened, what should we do about it?”

1) select which node the script is responding to
2) which event on the selected node will trigger the response
3) state the code you want to run when event occurs

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

What do [] square brackets mean in function and method syntax documentation?

A

square brackets in document denotes OPTIONAL. can be provided, but doesnt need to be provided.

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

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

A

element.addEventListener

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

What is a callback function?

A

a function passed into another function as an argument.

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

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

A

It is the event object, which contains all the information of that event.

17
Q

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

A

Is the element.target in which the event was dispatched at or originated from. You can find it on MBN

18
Q

What is the difference between these two snippets of code?

A

element.addEventListener( ‘click, handleClick);
this is just defining which function needs to be run. when an event happens.

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

the ‘function’ portion is going to need to be worked on first. and it will return the value to replace the [handleClick()]

19
Q

What is the className property of element objects?

A

this gets and sets the value of the class attribute of the selected element.

20
Q

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

A

document.querySelector(‘.class-name’).className = ‘New name’);

21
Q

What is the textContent property of element objects?

A

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

22
Q

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

A

document.querySelector(‘element-selected’).textContent = ‘New text content’;

23
Q

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

A

it does not always need an event listener

24
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

This would be a little more complicated. because we would always have to change the text content for each time we clicked it. instead of having an incremental increase to a variable

25
Q

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

A

so we are able to quickly call it by the variable name. instead of typing multiple methods.

26
Q

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

A

It is not part of the page yet, it is only creating a new element in Javascript. It will not be added until the 3rd step: appendChild

27
Q

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

A

the appendChild( ) method

28
Q

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

A

the name of the attribute and the value that is assigned to the attribute

If you want to assign a ‘class=”contact-form” ‘ it would be:

‘nameOfElement’.setAttribute(“class”, “contact-form”);

29
Q

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

A

1) creating the element using document.createElement( )
2) creating the text within that element using document.createTextNode( );
3) finding the location where you want to put the element.
document. querySelector(‘ ‘).appendChild( )

30
Q

What are two advantages of defining a function to do create something like the work of creating a DOM Tree

A

It makes the page dynamic, it could be repeated.

31
Q

What is the event.target?

A

The target the event happened on. more specific element it interacted with

32
Q

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

A

because of event Bubbling. the event starts at the most specific node and follows outwards.

33
Q

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

A

the type property.

element.type

34
Q

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

A

it takes the selectors / a string with matching css selector

and it returns the element which is teh closest ancestor of the selected element. if non, it is nul

querySelector looks going inward. element.Closest searches outward from the specific element point. l

35
Q

How can you remove an element from the DOM?

A

use the remove property method.

36
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

use an addEventListener on the containing element instead of every new element. ;