1.3.4 - Traversing the DOM Flashcards

1
Q

Getting the parent of a node

A

.parentElement and .parentNode

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

What does .parentElement?

its only readable?

A

returns the DOM node´s parent ELEMENT, or null if the node either has no parent, or its parent isn´t a DOM element

only readable

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

what does .parentNode?

only readable?

A

returns the parent of the specified node in the DOM tree .parentNode is the parent of the current node. The parent of an element is an Element node, a Document node, or a DocumentFragment node

readable only

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

It´s possible to chain parentNode?

A

YES

console.log(query.parentNode.parentNode)

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

When searching for childrens in the DOM, whitespace count as a node?

A

YES

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

When using .parentElement and .parentNode, how it will take whitespaces?

A

It will NOT take white space as a parent into consideration, it will take the element itself.

That is the reason why both properties will return almost the same thing

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

Why .parentElement and .parentNode will return almost the same thing?

A

because it´s unlikely that you will have an element who has a parent that is not also an element.

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

In the search for childrens, which 2 general options we have for searching in the DOM?

A

Selecting nodes of any type

Selecting only ELEMENT nodes

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

When we want to select child nodes of any type, which properties do we have?

A

.childNodes

.firstChild

.lastChild

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

When we want to select only element nodes, which properties do we have?

and can we count them?

A

.children

.firstElementChild

.lastElementChild

.childElementCount

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

What does .childNodes and in what category can we place it?

A

It gives us all the nodes, including white space as well, if we actually have white space between the elements in our HTML source code

category: select nodes of any type

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

What does .firstChild and in what category can we place it?

A

It gives us the first child, including white space as well. If the first child is a whitespace, it will get that

category: select nodes of any type

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

What does .lastChild and in what category can we place it?

A

It gives us the last child, including white space as well, if the last child is a white space, ti will get that

category: select nodes of any type

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

What does .children and in what category can we place it?

A

It gives us just ELEMENTS as the results(if we have whitespaces, it doesn´t care)

category: select only ELEMENT nodes

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

What does .firstElementChild and in what category can we place it?

A

It gives us the first child element, it does not care if it´s a whitespace

category: select only element nodes

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

What does .lastElementChild and in what category can we place it?

A

It gives us the last child element, it does not care if it´s white space.

category: select only element nodes

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

What does .childElementCount and in what category can we place it?

A

It gives us a count of all the children

category: select only element nodes

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

What problems can we face when selecting node types or element nodes?

A

If the document has whitespaces, when usint the “select node of any types” whitespaces will count as nodes, if we are using those selectors, and then the HTML source code gets compressed, we can have some errors, if we tried to select ELEMENTS using node type selectors.

If we are just focusing on selecting nodes and not elements, we can get completely different results

19
Q

How do we get the length of an array, and the node type?

A

var content = document.querySelector( ‘.content’ );

content. children.length
content. firstElementChild.nodeType
content. childNodes.length
content. firstChild.nodeType

20
Q

When selecting siblings, what major categories do we have?

A

selecting sibligns from

any node type

any element node

21
Q

What are the differences between selecting

any node type

any element nodes type

A

When selecting “any node type” are all nodes, including white spaces

When selecting “any element nodes types” are just nodes of the type ELEMENTS

22
Q

How can we select siblings of any node type?

A

using

.nextSibling

.previousSibling

23
Q

how can we select siblings of any node type of ELEMENT

A

using:

.nextElementSibling

.previousElementSibling

24
Q

document.documentElement

a qué hace referencia, que retorna?

A

a la etiqueta

< html >

25
Q

Cuando document.body puede ser/retornar null?

A

Si el script desde el cual invocamos document.body esta dentro de la etiqueta < head >, document.body es inaccesible, porque el navegador no lo leyó aún.

27
Q

Imaginemos mentalmente un div que tiene

un nodo padre,

dos nodos hermanos

y nodos hijos: todos los nodos hijos y el primer y último nodo hijo

Como lo podríamos diagramar, usando las propiedades para retornarlos?

28
Q

Podemos acceder elementos que aún no existen en el momento de ejecución

por ej: tratar de acceder a document.body desde la etiqueta < head >

30
Q

When we talk about Child nodes(or children), and Descendants, what are we meaning?

A

Child nodes(or children): elements that are direct children, they are nested exactly in the given one. for instance, “head” and “body” are children of “html”

Descendants: all elements that are nested in the given one, including children, their children and so on

31
Q

What methods can we use to check if we have child nodes?

A

elem.hasChildNodes()

32
Q

DOM collections are read-only?

33
Q

Why it´s better to use for…of to loop over a collection?

A

The for…in loop iterates over all enumerable properties, and collections have some “extra” rarely used properties that we usually don´t want to get

34
Q

It is a good practice to use for… in to loop over collections?

35
Q

What is better to use when we want to loop over a collection?

for…in or for… of?

37
Q

What are siblings?

example with head and body

A

nodes that are children of the same parent

“head” and “body” are siblings

38
Q

Talking about siblings…

“body” is said to be the … or … sibling of “head”

“head” is said to be the … or … sibling of “body”

A

“body” is said to be the next or right sibling of “head”

“head” is said to be the previous or left sibling of “body”

39
Q

When parentNode and parentElement returns something “different”

What is the exception?

A

document. documentElement.parentNode // document
document. documentElement.parentElement // null

40
Q

documentElement “html” is the root node?

41
Q

Whitch is the root node?

A

documentElement “html”

42
Q

Why using parentNode and parentElement on

document.documentElement

returns different things

A

Because “document” is the parent of documentElement, but “documentis not an element node,

43
Q

number of properties for accessing

all node types

name of the properties for accessing

all node types

A

6

parentNode, childNodes, firstChild, lastChid, previousSibling, nextSibling

44
Q

number of properties for accessing

all element nodes

name of the properties for accessing

all element nodes

A

6

parentElement, children, firstElementChild, lastElementChild, previousElementSibling, nextElementSibling

45
Q

If elem – is an arbitrary DOM element node…

Is it true that elem.lastChild.nextSibling is always null?

A

Yes, true. The element elem.lastChild is always the last one, it has no nextSibling, so if there are children, then yes.

46
Q

If elem – is an arbitrary DOM element node…

Is it true that elem.children[0].previousSibling is always null ?

A

No, wrong, because elem.children[0] is the first child among elements. But there may be non-element nodes before it. So previousSibling may be a text node.