JS230 Flashcards
Front-end development with JavaScript!
What is the DOM?
The DOM is an in-memory object representation of an HTML document.
What is the purpose of the DOM?
The DOM provides the functionality needed to build modern interactive user experiences.
What is document
?
document
is the top-most DOM node that represents the entire HTML document.
Node.nodeName
Returns the name of the current node as a string.
What does the .nodeName
property for elements return?
Returns the name of the corresponding tag in uppercase. EX: ‘P’ for paragraph.
Node.nodeType
Returns a number that returns a node type constant.
How would you determine if a node was an element node?
Use a comparison expression comparing node.nodeType
to Node.ELEMENT_NODE
.
How would you determine if a node was a text node?
Use a comparison expression comparing the node.nodeType
to Node.TEXT_NODE
.
An element node’s .nodeValue
property returns….
null
Node.nodeValue
Returns the value of a node.
A text node’s .nodeValue
property returns…
The textual content of the node.
What does the .textContent
property return for element nodes?
The .textContent
property returns a string of all the textual content of all the nodes inside the element.
Element
nodes represent…
HTML tags.
The best way to determine a node’s type from the console is…?
node.toString()
which will allow you to read the node’s name.
The best way to determine an Element
node’s type (tag it represents from HTML) in a program is…?
Use the instanceof
operator.
EX: node instance of HTMLParagraphElement
Node.childNodes
Returns a collection of the calling node’s children.
Node.firstChild
Returns the calling node’s first child OR null
.
Node.lastChild
Returns the calling node’s last child OR null
.
Node.parentNode
Returns the calling node’s parent node OR null
if the topmost node.
A live collection…
automatically updates to reflect changes in the DOM.
Node.nextSibling
Returns the node’s next sibling OR null
.
Node.previousSibling
Returns the node’s previous sibling OR null
.
What are HTML attributes?
Elements in HTML have attributes. Attributes are values that configure the elements or adjust their behavior in various ways to meet the criteria that users want.
Node.getAttribute()
Retrieves the value of the node instance’s attribute passed as an argument.
Node.setAttribute()
Sets the value of the node instance’s attribute (1st arg) to the value of the 2nd argument.
Node.hasAttribute()
Determines if the node instance has the attribute passed as an argument and returns an appropriate boolean value.
Reminder: There is a warning about using .setAttribute
to modify value
in XUL.
https://launchschool.com/lessons/ba24c7d4/assignments/c4315283#:~:text=When%20using%20setAttribute%20please%20take%20note%20of%20this%20warning%20from%20MDN
The DOM exposes special attributes as properties of the Element. What are these properties?
id
, name
, title
and value
Note: these properties can be accessed and reassigned using standard property access.
*these properties are only available on very select Elements.
Element.classList
property references…
A special array-like DOMTokenList
object that exposes useful properties and methods for working with an Element’s class(es).
Element.classList.add()
Adds a class name to an element
Element.classList.remove()
Removes a class list from an element.
Element.classList.toggle()
Adds a class to an element if it doesn’t exist and removes the class if it does.
Element.classList.contains()
Determines if the element has a class and returns an appropriate boolean.
Element.classList.length
Returns the number of classes to which the element belongs.
Element nodes have a style attribute accessible via Element.style
that references…
A CSSStyleDeclaration
Object:CSSStyleDeclaration {alignContent: "", alignItems: "", alignSelf: "", alignmentBaseline: "", all: "", ...}
To remove a CSS property…
set the property to null
via Element.style
Node.getElementsByTagName(tagName)
Returns an array-like collection of matching elements that are within the calling node.