The DOM Flashcards

1
Q

How would you check whether a variable or property references a jQuery object?

A

console.log($content.jquery);

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

Finally in a try catch statement has which value when arguments are passed in?

A

finally takes no arguments so even if one is passed it will be set to undefined.

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

What is the value of this within a function expression in the context of the Event object

A

The event.currentTarget

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

Can you create a clone from an element with an id attribute?

A

Cannot create a clone where there’s an id attribute anywhere on the cloned element.

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

What does textContent return?

A

It grabs all the text within an element, not just up to the next tag and combines it into a single string. It also includes all the whitespace which leads to excess whitespace.

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

What does nodeValue return?

A

null for elements and the text for text nodes.

For text nodes it returns text up to the next tag it encounters, whether a closing or opening one.

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

DOM nodes have different types. Mention two

A

Elements (HTML tags) and Text Nodes

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

What is the property ‘nodeName’ ?

A
A property that contains a string with the name of the type of the node.
For Elements (anything that represents an HTML tag), this is the name of the corresponding tag in uppercase. The use of uppercase is a historical throwback to a time when standard practice was to write uppercase HTML tags; contemporary HTML uses lowercase tags, but this method still returns uppercase names.

let p = document.querySelector(‘p’);
> p.nodeName
= “P”

For text nodes – even empty nodes – the nodeName is “#text”. For comments, it’s “#comment”.

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

What does the node property ‘nodeType’ contain?

A

A number that represents the type of node. The number corresponds to a constant

let p = document.querySelector(‘p’);
> p.nodeType
= 1

> p.nodeType === Node.ELEMENT_NODE
= true
> 1 === Node.ELEMENT_NODE
= true
> document.nodeType === Node.DOCUMENT_NODE
= true
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does the node property ‘nodeValue’ contain?

A

A text node returns a string with the value of a node up to next closing or opening tag. Elements don’t have any values and return null

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

What does the node property ‘textContent’ contain?

A

The text content of all the children of an element. It’s all concatenated into a single string and includes all the whitespace

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

What does ‘eventTarget’ do?

A

It provides event handling behaviour to interact with the page

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

What does calling toString() on an anchor element do and what should we use instead?

A

It returns the url of the anchor.

let a = document.querySelector('a');
a
<a href="http://domain.com/page">Page</a>
a.toString();
=> "http://domain.com/page"
We should instead call the constructor on the anchor element.
let a = document.querySelector('a').constructor;
=> function HTMLAnchorElement() { [native code] }

it references a function that creates Objects of the appropriate Element type. The value is browser-dependent, though, which adds some clumsiness when using it.

Chrome:
> document.querySelector(‘a’).constructor;
= function HTMLAnchorElement() { [native code] }

Firefox:
> document.querySelector(‘a’).constructor;
= function()
> document.querySelector(‘a’).constructor.name; /* Note use of .name property! */
= “HTMLAnchorElement”

SafariCopy:
> document.querySelector('a').constructor;
= function HTMLAnchorElement() {
      [native code]
  } = $1

Edge:
> document.querySelector(‘a’).constructor;
= function HTMLAnchorElement() { [native code] }

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

On determining the node type which technique should you use in which environment?

A

In the console use String or toString(). In the code file itself use the instanceof operator or the tagName property.
With element nodes, tagName returns the same value as nodeName.

> let p = document.querySelector('p');
> p instanceof HTMLParagraphElement;
= true
> p instanceof HTMLAnchorElement;
= false
> p instanceof Element;
= true

The downside here is that you have to test against a particular Element type. If all you need to know, though, is whether a node is an Element, you can test it with instanceof Element:

> p instanceof HTMLElement;
= true
> p instanceof Element;
= true
> p instanceof Node;
= true
> p instanceof SVGElement;
= false
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How would we extract the string referenced by ‘class’?
Ex:
class=“btn btn-lg btn-primary”

A
let button = document.querySelector('button');
button.className // =>  "btn btn-lg btn-primary"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

classList references a special array-like DOMTokenList object that has these properties and methods:

A
Name	Description
add(name)	Add class name to element
remove(name)	Remove class name from element
toggle(name)	Add class name to element if it doesn't exist, remove if it does exist
contains(name)	Return true or false depending on whether element has class name
length	The number of classes to which element belongs
17
Q

What are DOM levels?

A

Specifications by the W3C that say which features should be supported by browsers

18
Q

All DOM objects are ___
They inherit from ___
The two most common types are ___ and ___

A

Nodes
Node
Element and Text

19
Q

What is the difference between EventTarget and Node?

A

EventTarget provides the event handling behaviour to interact with the page. Node provides behaviour shared by all nodes

20
Q

Text and Element are subtypes of ___

A

Node

21
Q

Elements represent ___

A

HTML tags

22
Q

Warning for when using setAttribute

A

Using setAttribute to modify certain attributes, most notably ‘value’ in XUL, works inconsistently, as the attribute specifies the default value. To access or modify the current values, you should use the properties. For example, use Element.value instead of Element.setAttribute.

23
Q

The Browser Object Model (BOM) lets you access components beyond what is displayed on the page. What are they?

A

The windows used to display web pages
The browser’s history
Sensors, including location

24
Q

Set the class attribute ‘heading’ to primaryHeading with setAttribute()

A

primaryHeading.setAttribute(‘class’, ‘heading’)

25
Q

If you create a clone with p.cloneNode(true)

and you go one to change the original after the copy was made is the change reflected in the copy and vice versa?

A

No. They are independent objects

26
Q

What is the best strategy for updating text with JavaScript?

A

It is to place the text you need to update within an element; the element type doesn’t matter – even a bare span or div element will suffice. This approach makes using textContent simpler and safer to use.

27
Q

What is a side effect of replacing the value of an element’s text-content?

A

It removes all child nodes from the element and replaces them with a text node that contains the value:

28
Q

Can you use child and parent Element properties in all browsers by calling them on document?

A

No. Most browsers include these properties in document, which has type Document. Internet Explorer does not, though; it provides them in document.body instead. You can use document.body with them all and get the expected results.

29
Q

Is the childNodes property live or not?

A

It’s live

30
Q

Clicking cancel on a prompt returns…

A

null

31
Q

What is the value of ‘this’ within the event handler when using a function declaration?

A

It’s the element to which the event handler was attached

this === event.currentTarget

32
Q

What’s the difference between event.target and event.currentTarget?

A

event.target is the element that triggered the event. even.currentTarget is the element that the event handler was attached to

33
Q

Aside from OOP what is another meaning of Execution Context?

A

An Execution Context is an abstract concept of an environment where the JavaScript code is evaluated and executed. Whenever any code is run in JavaScript, it’s run inside an execution context.

34
Q

Define “single page application”

A

An application that relies solely on js and the XHR API to build the DOM