DOM Element Object & JS Scope Flashcards
.appendChild()
.replaceChild()
.removeChild()
methods to add/replace/remove children nodes to an element
.innerHTML
.innerText
.textContent
element.innerHTML //sets or returns the content of an element
//will process HTML use carefully
element.innerText //the text content without text spacing and tags, except script and style elements
element.textContent //sets or returns only textual content of a node
.id
.className
.tagName
.classList
.id //sets or returns the element id
.className //sets or returns the element class
.tagName // read-only returns the tag name
.classList // returns a list of the element’s class names
.focus()
.blur()
element.focus() //gives focus to an element
document.getElementById(“id”).focus();
element.blur() //removes focus from an element
element.nodeName
element.nodeType
element.nodeValue
.nodeName // returns the name of a node header, etc.
.nodeType // returns the type of a node
.nodeValue // sets or returns the value of a node (text between tags)
element.style
sets or returns style attribute of an element
element.style.attribute = “”;
element.remove()
removes the element from the DOM
element.setAttribute();
element.removeAttribute();
//sets / changes the attribute for an element
element.setAttribute(“class”, “demo”);
element.removeAttribute(“href”);
element.appendChild(“li”);
appends a child element
element.click();
performs a click event on the element
element.title
sets or returns the value of the title attribute
What are the three possible scopes of variables?
Block
Function | Local
Global
Block Scope
variables declared inside a { } block
that cannot be accessed outside of it
{
let x = 2;
}
instantiated with let or const
a nested block can access the variable if it comes after.
introduced in ES6
Local Scope
Variables declared within a JavaScript function, become LOCAL to the function.
function name() {
let myName = ‘Joe’;
}
only exists within the function
Local variables are created when a function starts, and deleted when the function is completed.
Function Scope
JavaScript has function scope: Each function creates a new scope.
Variables defined inside a function are not accessible (visible) from outside the function.
Variables declared with var, let and const are quite similar when declared inside a function. As they’ll all have function scope.
Global Scope
Global variables can be accessed from anywhere in a JavaScript program.
DOM collections also have global scope
Window object has global scope
Assigned variables that are not declared have global scope
function name() {
carName = ‘Volvo’; //global scope
}
Why are global variables dangerous?
Your global variables (or functions) can overwrite window variables (or functions).
Any function, including the window object, can overwrite your global variables and functions.
Global variables can bleed out of conditional (if) blocks.
What values are falsy?
false,
zero: 0, 0.0, 0x0, -0, 0n
(string.length === 0): “” `` ‘’
null
undefined
NaN
Objects are falsy if and only if they have the [[IsHTMLDDA]] internal slot, only document.all has this.
What values are truthy?
true,
existing objects: ({}), ([]), (new Date())
non-zero numbers: 42, -2, Infinity, 12n
(string.length > 0): “0”, “text”, “false”