DOM and BOM Flashcards

1
Q

What does BOM stand for?

A

Browser Object Model.

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

What does DOM stand for?

A

Document Object Model.

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

What does the BOM provide us?

A

It provides us with the window object and associated methods.

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

Name a BOM method.

A

alert is an example of a BOM method.

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

How can we access the BOM methods

A

We can call them either by window.methodName or just methodName (without the window precursor)

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

How can we examine the DOM in a web-browser.

A

We can access it in the Elements tab of the developer tools

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

How can we access the DOM using JavaScript?

A

We can access it by using document

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

When should we use document.querySelector(‘selectorName’)

A

document.querySelector should be used when the selector is more complicated than other targeted selectors.

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

What is the syntax for accessing a DOM element using querySelector?

A

document.querySelector(‘selectorName’)
selectors must be prepended with # or . if they are ids or classes (or [] for attributes)

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

If there are multiple elements available to querySelector which will be retrieved?

A

Only the first encountered element matching the querySelector will be retrieved.

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

Can we combine elementNames in querySelector?

A

Yes, we can combine them in a long string (including # or . as appropriate)

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

How can we search inside an element for another element using querySelector?

A

We can create a variable for an initial querySelector for the outside element then use that as
variable.querySelector(‘innerElementName’)

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

How can we select something from the DOM using an attribute selector?

A

document.querySelector(‘[data-type=attributeName]’);

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

What must things must we remember when selecting an object from the DOM using querySelector?

A
  1. selection criteria must be in single quotes
  2. selection criteria must include the CSS indicator for class or id
  3. selection will include only the first element found that matches
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What symbols need to be included when using querySelector to find an attribute?

A

Square brackets - []

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

How do we access the classList property for an element in the DOM?

A

We would need to selected the element in the DOM and save it to a variable.

let paragraph = document.querySelector(‘p’)

paragraph.classList would then access the p classes

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

Why would we need to access the classList of an element?

A

We access the classList of an element to change the CSS classes applied to it, thereby changing its style rendering

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

How do we add a CSS class to an element in the DOM using JavaScript?

A

We save the element to a variable to access the classList property

then we variableName.classList.add(‘className’)

to add a className to an element

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

Can you add multiple classes to an element using classList.add?

A

Yes, but it might not be considered a best practice

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

When adding and removing classes to an element, do we need a CSS class indicator ( . ) passed in as an argument?

A

No, we simply need to pass in the class name in quotes as an argument

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

How would we remove a class from an element in the DOM?

A

We would save the element to a variable using

variableName = document.querySelector(‘elementName’)

then variableName.classList.remove(‘className’)

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

Can we add or remove more than one class using

classList.add or classList.remove?

A

Yes, by separating them with a comma and surrounding each one with quotation marks

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

Can we check is an element has a certain class assigned to it?

A

Yes, we use classList.contains to get a Boolean value back

we can then use this in a conditional statement to change state

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

Name two ways we could toggle the presence of a class on an element in the DOM

A
  1. write a conditional statement based on classList.contains
  2. use classList.toggle
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

How can we toggle the presence of a value on and off an element in the DOM?

A

We can use classList.toggle

or

we can write a conditional statement with classList.contains

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

What are event listeners?

A

Event Listeners are methods that we call on a DOM element that monitors that element for events (click, keyup, mousein,etc)

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

How do we use an event listener in JavaScript?

A

We append the addEventListener method to the element we want to monitor. We pass the event we are listening for and a function as arguments to the addEventListener method.

elementName.addEventListener(‘eventName’, function(){})

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

What is another name for the function passed to an event listener as an argument.

A

This function is also known as the event handler.

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

What does the event handler allow us to do?

A

The event handler is a function that allows us to run code when an event listener fires

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

How can we test if an event listener works?

A

We can test an even listener by having the event handler console.log some message.

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

How can we use an event listener to change the color or background color of a DOM element?

A
  1. Create classes to make the change
  2. Create variables to hold the elements we want to change with document.querySelector(‘elementName’)
  3. Create a variable to hold the element on which we will add an event listener
  4. elementName.addEventListener(‘eventName’, function(){});
  5. In the event handler, use the .classList.add to add the appropriate CSS classes to the appropriate element to change.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

Is the event handler a callback function?

A

Yes, the event handler is a callback function

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

Does the event handle accept arguments?

A

The event handle only accepts one argument - the event object.

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

What is the typical name for the event object passed to the event hander?

A

It is typical to name the event object either:

e or evt

35
Q

How is querySelectorAll used?

A

querySelectorAll is used to select all the elements in a DOM that match the criteria.

36
Q

What does querySelectorAll return to us?

A

querySelectorAll returns a NodeList - an array-like object.

37
Q

What two properties does a NodeList have?

A

NodeLists have lengths and numbers as ‘keys’

38
Q

How do we influence nodes on a NodeList?

A

We can loop over the nodes on a NodeList with either for…of or forEach loops.

39
Q

What might be a problem when using forEach on a NodeList?

A

The forEach method may not work on older browsers.

40
Q

How can we turn a NodeList into an array?

A

We can use Array.from to create an array from a NodeList:

let array = Array.from(NodeList)

41
Q

How can we access a certain position on a NodeList?

A

NodeLists are array like objects, so we can use index-like notation to access a position on a NodeList

42
Q

What are alternatives to querySelectorAll?

A

getElementsByTagName

getElementsByClassName

43
Q

Why might be use the getElements selector instead of a querySelectorAll?

A

getElements can be faster

getElements return a live list

44
Q

What is a ‘live list’ in regards to selecting DOM elements?

A

It is a list of DOM nodes that is updated if more elements/classes are added to the DOM.

45
Q

What is another name for a ‘live list’?

A

A ‘Live Collection’.

46
Q

How many types of Nodes are there?

A

There are 7 types of Nodes.

47
Q

What are the types of Nodes we will encounter in the DOM?

A
  1. Element Node
  2. Text Node
  3. Processing Instruction Node
  4. Comment Node
  5. Document Node
  6. Document Type Node
  7. Document Fragment Node
48
Q

What are the two most commonly used methods on Nodes?

A
  • Node.parentElement
  • Node.textContent
49
Q

What does the parentElement method do to a node?

A

It returns the parent element of a node.

If the node has no parent or the parent is not a node - it returns null

50
Q

What does the textContent method do to a node?

A

The textContent method sets or returns the textual content of an element and all descendants

51
Q

What is the danger of using Node.textContent to set the textual content of a node?

A

Using textContent will removes all the node’s children and replaces them with a single text node and the provided string.

52
Q

What is the difference between an Element and a Node?

A

An element is a type of Node. Elements are specified directly in the HTML with a tag. They can also have id’s or classes

53
Q

When should you use ID to select elements in the DOM vs classes or attributes?

A

When you only need to select one element, use ID. When you select more than one element use either classes or attributes.

54
Q

What is a JavaScript hook in relation to DOM manipulation?

A

A hook is a way of indicating that JavaScript is needed for a component work by prepending a class with js.

55
Q

Aside from classList, how might we influence an element’s CSS properties?

A

We can use:

  1. element.style.propertyName = value
  2. element.style.setProperty(‘property’, ‘value’)
56
Q

How can we set a custom-property value using JavaScript?

A

We can use:

element.style.setProperty(‘–property’, ‘value’)

57
Q

What do we need to consider when trying to access CSS styles on elements with JavaScript?

A

We need to consider where the style rule is coming from:

Inline or Computed

58
Q

What are the two ways we can access CSS style information about an element using JavaScript?

A

After saving the element as a variable we could use.

  1. element.style.styleName
  2. getComputedStyle(elementName)
59
Q

What will getComputedStyles(variable) return to us?

A

It will return an object will all computed styles.

60
Q

What are three requirements of custom attributes?

A
  1. Start with the prefix data-
  2. No capital letters
  3. Contain only a-z, numbers, - , . , : , _
61
Q

How are custom attributes useful

A

They allow us

  1. to add extra information when building components
  2. create extra functionality on elements
62
Q

How can we set attributes on elements using JavaScript?

A

element.setAttribute(‘attributeName’, ‘value’)

63
Q

How do we retrieve an attribute from an element using JavaScript?

A

We use the getAttribute method:

element.getAttribute(‘attributeName’)

64
Q

What will the getAttribute method return?

A

It will return a string of the value, if present, or null or “ ” if not present.

65
Q

Give an example of an HTML tag built-in attribute.

A

Id, class, contenteditable are all examples of attributes in relation to HTML tags.

66
Q

How can we remove an attribute from an HTML tag?

A

We can use the .removeAttribute method on an HTML element.

67
Q

What is the syntax for the removeAttribute method?

A

element.removeAttribute(‘attribute name’)

68
Q

What method can we use to get size and position information about an element?

A

We can use the

getBoundingClientRect()

method with our element.

69
Q

What values does the getBoundingClientRect() method return to us?

A

It returns:

  1. Top
  2. Left
  3. Bottom
  4. Right
  5. Width
  6. Height
  7. X
  8. Y
70
Q

Where are the values of Top, Bottom, Left, Right measured from in getBoundingClientRect()?

A

They are measured from the top of the document to the stated position on the element.

71
Q

What is meant by “Traversing the DOM”?

A

Traversing the DOM means moving from one element to another using various methods.

72
Q

How can we move downwards from an element in the DOM?

A

We can use:

  1. querySelector or querySelectorAll
  2. children
  3. firstElementChild
73
Q

When we use the children property of an element, what is returned?

A

The children property returns an HTML Collection of all direct descendants.

74
Q

What might we need to do to iterate over an HTML Collection?

A

We might need to change it into an array with Array.from(CollectionName)

75
Q

What will the firstElementChild select?

A

It will select the first child of the element indicated:

element.firstElementChild

76
Q

What are two ways to traverse the DOM upwards?

A
  1. parentElement
  2. closest
77
Q

How is the parentElement selector used for traversing the DOM upwards?

A

element.parentElement will select the parentElement and this can be saved to a variable.

78
Q

How is the closest selector used when traversing the DOM?

A

The closest selector will find and select the closest ancestor of the indicated element:

element.closest(elementToFind)

79
Q

Can you use closest to find the closest element across or downwards in the DOM?

A

No, closest only searches upwards in the DOM tree.

80
Q

Name three ways to traverse the DOM sideways

A
  1. nextElementSibling
  2. previousElementSibling
  3. combining parentElement, children, and index
81
Q

Why would you use parentElement, children, and index to traverse the DOM sideways?

A

This combination will allow you to select a specific sibling, as opposed to adjacent siblings.

82
Q

Why do we set target elements on items in the DOM?

A

This allows us to link two elements together by using a data-target value equal to another element’s Id value.

83
Q

How do we link an event listener with another element using target elements

A

const target = tab.dataset.target const tabContent = tabby.querySelector(‘#’ + target)

84
Q

What is the syntax for a dataset element when using it as a target element?

A

data-target=”targetName”

targetName should match the idName of the element we are targeting