Browser: Document, Events, Interfaces Flashcards
What is DOM?
Document Object Model, or DOM for short, represents all page content as objects that can be modified. It is an interface that treats HTML or XML document as a tree structure, where each node is an object of the document.
The document object is the main “entry point” to the page. We can change or create anything on the page using it.
What is a DOM element?
An element is a node of a specific type — element (Node.ELEMENT_NODE).
In simple words, an element is a node that’s written using a tag in the HTML document… ex. h2 & p are all elements because they are represented by tags.
The document type, the comment, the text nodes aren’t elements because they are not written with tags
What is the topmost tree node?
The topmost document node is document.documentElement. That’s the DOM node of the html tag.
How would you grab all child nodes of node?
The childNodes collection lists all child nodes, including text nodes
Would get children of document.body-
document.body.childNodes
Will return a collection- a special array-like iterable object that we can use for..of to iterate over, but not array methods (unless we use Array.from)
What is a quick way to grab a first or last child node?
elem.firstChild and elem.lastChild
elem. childNodes[0] === elem.firstChild
elem. childNodes[elem.childNodes.length - 1] === elem.lastChild
How would you grab the nearest siblings in both directions?
elem. previousSibling
elem. nextSibling
How do you find the parent node?
elem.parentNode
How would you grab all child element nodes? First and last element child?
Neighbor elements? Parent element?
This does not include things like text nodes or comment nodes.
elem. children
elem. firstElementChild
elem. lastElementChild
elem. previousElementSibling
elem. nextElementSibling
elem. parentElement
What is the difference between innerHTML and textContent?
The innerHTML property allows to get the HTML inside the element as a string.
The textContent provides access to the text inside the element: only text, minus all .
In most cases, we expect the text from a user, and want to treat it as text. We don’t want unexpected HTML in our site. An assignment to textContent does exactly that.
What does the hidden property do?
The “hidden” attribute and the DOM property specifies whether the element is visible or not. Technically, hidden works the same as style=”display:none”. But it’s shorter to write.
How do you read the text from a text node?
nodeValue/data
Hello
let text = document.body.firstChild; alert(text.data); // Hello
let comment = text.nextSibling; alert(comment.data); // Comment
If an attribute is non-standard, is there a way to access such attribute? If so, what are the methods to…
1) check existence
2) get value
3) set value
4) remove attribute
1) elem.hasAttribute(name) – checks for existence
2) elem.getAttribute(name) – gets the value
3) elem.setAttribute(name, value) – sets the value
4) elem.removeAttribute(name) – removes the attribute
How can you read all attributes of an element with name and value properties?
elem.attributes
Returns a collection of objects that belong to a built-in Attr class, with name and value properties
Use example:
for (let attr of elem.attributes) {
alert( ${attr.name} = ${attr.value}
);
}
How do you select an element by an attribute?
Using brackets
Example:
document.querySelector(‘[data-widget-name]’)
What are the differences between attributes and properties?
Properties can have any value, whereas attributes are strings
Properties are case-sensitive, whereas attributes are not