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.