DOM Flashcards

1
Q

What is a “model”?

A

A representation of an object/something

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 document for that web application

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

The model created is made out of objects in the JavaScript language.

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

What is a DOM Tree?

A

Each branch of the tree ends in a node, and each node contains objects.

The Document Object Model (DOM) connects web pages to scripts or programming languages by representing the structure of a document in memory.

A model of the web page and stored in browsers’ memory. / Element and all its children, and any data in the folder??

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

‘getElementById()’ and ‘querySelector()’

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

‘getElementsByClassName()’ or ‘getElementsByTagName()’ or ‘querySelectorAll()’

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

Store location of element and don’t have to look for it again

So you can access and work on the same element node more than once.

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

dir

‘console.dir()’

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

Why would a ‘script’ tag need to be placed at the bottom of the HTML content instead of at the top?

A

The elements need to load first and when try to access through javascript, the elements exist first

The browser needs to parse all of the elements in the HTML page before the JavaScript code can access them.

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

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

A

Takes CSS selector as argument and returns the first matching element.

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

What does ‘document.querySelectorAll()’ take as its argument and what does it return?

A

Takes CSS selector as argument and returns a NodeList of all elements with argument.

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

Events are things that happen; event handlers are functions we provide, work that should happen when events happen; so developers can respond to visitors actions

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

Means optional

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

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

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

Event object which contains all information about the 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

The target property of the Event interface is a reference to the object onto which the event was dispatched. (property where the event originates from?)

MDN

18
Q

What is the difference between these two snippets of code?

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

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

A

The second will call the function handleClick when the page loads. The first will call the function when the event fires.

2nd: function is going to be run immediately; even before event

19
Q

What is the ‘className’ property of element objects?

A

Property holding class attribute of element

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

20
Q

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

A

object.className = ‘string’

var variable-name = document.querySelector(‘class-name’);
variable-name.className = ‘new-class-name’;

21
Q

What is the ‘textContent’ property of element objects?

A

Text in containing element and its children

The textContent 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

object.textContent = ‘newvalue’

var variable-name = document.querySelector(‘class-name’);
variable-name.textContent = ‘new strings’

23
Q

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

A

Not always you don’t always need information about event — function(EVENT)

24
Q

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

A

It is more work for the browser to not use a variable

25
Q

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

A

No, the result of method has to be added as a child of an existing element.

26
Q

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

A

element.appendChild()

27
Q

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

A

(‘attribute-name’, ‘attribute-text/value/variable’)

28
Q

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

A
  1. createElement 2. Grab spot to append to 3. Append it
  2. Create new element: .createElement()
  3. Give it content: .createTextNode()
  4. Add it to DOM: .appendChild()
29
Q

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

A

Assign or append text content to element; get the text content and set text content

Represents the text content of the node and its descendants.

30
Q

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

A
  1. Create new element and .setAttribute(‘class’, value) method
  2. Query the Dom and assign new class to .className property
31
Q

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

A
  1. Repeat code block without retyping
  2. Make it dynamic, as long as the data is there, the work will be done
  3. Names the purpose of the code block
32
Q

What is the ‘event.target’?

A

Property where event trigger is from

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

33
Q

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

A

Default type of event flow is Event Bubbling. Event starts at most specific node and flows outward to least specific one.

34
Q

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

A

event.target.tagName property

35
Q

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

A

Argument: ‘selector’

Returns: Will return itself or the nearest matching ancestor.

36
Q

How can you remove an element from the DOM?

A

.remove() method

37
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 event delegation. Add event listener to the parent element holding the elements you want to listen for.