Javascript Document Flashcards

(66 cards)

1
Q

is the root “abstract” class for everything.

Objects of that class are never created. It serves as a base, so that all DOM nodes support so-called “events”, we’ll study them later.

A

Event Target

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

is also an “abstract” class, serving as a base for DOM nodes.

It provides the core tree functionality: parentNode, nextSibling, childNodes and so on (they are getters). Objects of Node class are never created. But there are other classes that inherit from it (and so inherit the Node functionality).

A

Node

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

The document global object belongs exactly to this class. It serves as an entry point to the DOM.

A

Document

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

an “abstract” class, inherited by:

Text – the class corresponding to a text inside elements, e.g. Hello in <p>Hello</p>.
Comment – the class for comments. They are not shown, but each comment becomes a member of DOM.

A

Character Data

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

is the base class for DOM elements.

It provides element-level navigation like nextElementSibling, children and searching methods like getElementsByTagName, querySelector.

A

Element

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

is the basic class for all HTML elements. We’ll work with it most of the time.

It is inherited by concrete HTML elements:

HTMLInputElement – the class for <input></input> elements,
HTMLBodyElement – the class for <body> elements,
HTMLAnchorElement – the class for <a> elements,</a>

A

HTMLElement

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

alert( document.body instanceof HTMLBodyElement ); // true
alert( document.body instanceof HTMLElement ); // true
alert( document.body instanceof Element ); // true
alert( document.body instanceof Node ); // true
alert( document.body instanceof EventTarget ); // true

A

instanceOf

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

property provides one more, “old-fashioned” way to get the “type” of a DOM node.

A

nodeType

let elem = document.body;

// let’s examine: what type of node is in elem?
alert(elem.nodeType); // 1 => element

// and its first child is…
alert(elem.firstChild.nodeType); // 3 => text

// for the document object, the type is 9
alert( document.nodeType ); // 9

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

Given a DOM node, we can read its _____ from _______ or tagName properties:

A

nodeName, tagName
alert( document.body.nodeName ); // BODY
alert( document.body.tagName ); // BODY

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

For instance, let’s compare tagName and nodeName for the document and a comment node:

A
<script>
// for comment
    alert( document.body.firstChild.tagName ); // undefined (not an element)
    alert( document.body.firstChild.nodeName ); // #comment

    // for document
    alert( document.tagName ); // undefined (not an element)
    alert( document.nodeName ); // #document
</script>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

property allows to get the HTML inside the element as a string.

A

innerHTML

alert( document.body.innerHTML ); // read the current contents
document.body.innerHTML = ‘The new BODY!’; // replace it

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

“innerHTML+=” does a full overwrite

A

true

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

In other words, innerHTML+= does this:

A

The old contents is removed.
The new innerHTML is written instead (a concatenation of the old and the new one).

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

property contains the full HTML of the element. That’s like innerHTML plus the element itself.

A

outerHTML

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

Beware: unlike innerHTML, writing to outerHTML does not change the element. Instead, it replaces it in the DOM.

A

True

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

The outerHTML assignment does not modify the DOM element (the object referenced by, in this case, the variable ‘div’), but removes it from the DOM and inserts the ___ _____ in its place.

A

new HTML

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

property is only valid for element nodes.

A

innerHTML

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

Other node types, such as text nodes, have their counterpart: _____ and ____ properties

A

data

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

provides access to the text inside the element: only text, minus all <tags>.</tags>

A

textContent

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

specifies whether the element is visible or not.

A

“hidden” attribute

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

DOM nodes are regular JavaScript objects. We can alter them.

A

True

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

For instance, let’s create a new property in document.body

A

document.body.myData = {
name: ‘Caesar’,
title: ‘Imperator’
};

alert(document.body.myData.title); // Imperator

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

We can add a method as well:

A

document.body.sayTagName = function() {
alert(this.tagName);
};

document.body.sayTagName(); // BODY (the value of “this” in the method is document.body)

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

So when an element has id or another standard attribute, the corresponding property gets created. But that doesn’t happen if the attribute is ____-_______

A

non-standard

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Please note that a standard attribute for one element can be unknown for another one.
IMPORTANT
26
So, if an attribute is non-standard, there won’t be a DOM-property for it. Is there a way to access such attributes?
elem.hasAttribute(name) – checks for existence. elem.getAttribute(name) – gets the value. elem.setAttribute(name, value) – sets the value. elem.removeAttribute(name) – removes the attribute.
27
DOM properties are not always strings. For instance, the input.checked property (for checkboxes) is a ______
boolean
28
There are other examples. The style attribute is a string, but the style property is an _______
object
29
All attributes starting with “____” are reserved for programmers’ use. They are available in the dataset property.
data-
30
For instance, if an elem has an attribute named "data-about", it’s available as _____________
elem.dataset.about
31
To create DOM nodes, there are two methods:
document.createElement(tag) Creates a new element node with the given tag: document.createTextNode(text) Creates a new text node with the given text:
32
Creating the message div takes 3 steps:
// 1. Create
element let div = document.createElement('div'); // 2. Set its class to "alert" div.className = "alert"; // 3. Fill it with the content div.innerHTML = "Hi there! You've read an important message.";
33
To make the div show up, we need to insert it somewhere into document. For instance, into element, referenced by document.body
There’s a special method append for that: document.body.append(div).
34
we can call append method on any other element, to put another element into it. For instance, we can append something to
by calling div.append(anotherElement).
True
35
append nodes or strings at the end of node
node.append(...nodes or strings)
36
insert nodes or strings at the beginning of node
node.prepend(...nodes or strings)
37
insert nodes or strings before node
node.before(...nodes or strings)
38
node.after(...nodes or strings)
insert nodes or strings after node
39
replaces node with the given nodes or strings
node.replaceWith(...nodes or strings)
40
elem.insertAdjacentHTML(where, html)
"beforebegin" – insert html immediately before elem, "afterbegin" – insert html into elem, at the beginning, "beforeend" – insert html into elem, at the end, "afterend" – insert html immediately after elem
41
elem.insertAdjacentText(where, text)
the same syntax, but a string of text is inserted “as text” instead of HTML
42
elem.insertAdjacentElement(where, elem)
– the same syntax, but inserts an element.
43
To remove a node, there’s a method
node.remove()
44
All insertion methods automatically remove the node from the old place.
True
First
Second
45
creates a “deep” clone of the element – with all attributes and subelements.
elem.cloneNode(true)
46
then the clone is made without child elements.
elem.cloneNode(false)
47
JavaScript can modify both classes and style properties.
True
48
adds/removes the class
elem.classList.add/remove("class")
49
adds the class if it doesn’t exist, otherwise removes it.
elem.classList.toggle("class")
50
checks for the given class, returns true/false.
elem.classList.contains("class")
51
For multi-word property the camelCase is used:
background-color => elem.style.backgroundColor z-index => elem.style.zIndex border-left-width => elem.style.borderLeftWidth
52
elem.style.removeProperty('style property')
remove property
53
document.body.style.background = 'red'; //set background to red setTimeout(() => document.body.style.removeProperty('background'), 1000)
// remove background after 1 second
54
getComputedStyle(element, [pseudo])
Element to read the value for. A pseudo-element if required, for instance ::before. An empty string or no argument means the element itself. The result is an object with styles, like elem.style, but now with respect to all CSS classes.
55
is the nearest ancestor that the browser uses for calculating coordinates during rendering.
offsetParent
56
provide x/y coordinates relative to offsetParent upper-left corner.
offsetLeft/offsetTop
57
the outer width of element
offsetWidth
58
the outer height
offsetHeight
59
Inside the element we have the borders. To measure them, there are properties
clientTop and clientLeft
60
These properties provide the size of the area inside the element borders.
clientWidth/Height
61
These properties are like clientWidth/clientHeight, but they also include the scrolled out (hidden) parts
scrollWidth/Height
62
scrollWidth/Height
scrollLeft/scrollTop
63
To get window width and height, we can use the
clientWidth/clientHeight of document.documentElemen
64
scrolls the page relative to its current position. For instance, scrollBy(0,10) scrolls the page 10px down.
The method scrollBy(x,y)
65
scrolls the page to absolute coordinates, so that the top-left corner of the visible part has coordinates (pageX, pageY) relative to the document’s top-left corner. It’s like setting scrollLeft/scrollTop
The method scrollTo(pageX,pageY)
66
scrolls the page to make elem visible. It has one argument:
The call to elem.scrollIntoView(top) top = true/false