Think about the DOM tree structure of this page.
What would you draw this like?
How would you examine the elements of this page?
How would you add elements to the page?
How would you remove elements from the page?
Set a breakpoint in the initialize()
function and refresh the page.
Then toggle the this: window
object open to discover its contents
We already know how to get an element of the DOM using document.getElementById()
,
document.querySelector()
and document.querySelectorAll()
We also know we can add things to the tree using appendChild(node)
on
a DOM element.
The DOM also has
Name | Description |
---|---|
document.createElement("tag") | creates and returns a new empty DOM node representing an element of that type |
document.createTextNode("text") | creates and returns a text node containing given text |
// create a new <h2> node
let newHeading = document.createElement("h2");
// Add text to the node method 1
let newText = document.createTextNode("This is a new heading!")
newHeading.appendChild(newText);
// Add text to the node method 2
newHeading.innerText = "This is a newer heading!";
document.appendChild(newHeading);
Every node's DOM object has the following (read-only) properties:
Name(s) | Description |
---|---|
firstChild, lastChild | start/end of this node's list of children, may include text or comments |
firstElementChild, lastElementChild | start/end of this node's list of children elements |
childNodes | array of all of this node's children |
nextSibling, previousSibling | neighboring nodes with the same parent, including whitespace nodes |
nextElementSibling, previousElementSibling | neighboring element nodes with the same parent, skipping whitespace nodes |
parentNode, | the element that contains this node |
Complete list of DOM node properties
Browser incompatibility information (IE6 sucks)
<p id="foo">
This is a paragraph of text with a
<a href="page.html">link</a>.
</p>
<div>
<p id="foo">
This is a paragraph of text with a
<a href="page.html">link</a>.
</p>
</div>
Q: How many children does the div
have?
A: 3
<p>
Q: How many children does the paragraph have? A: 3 (text, a, text)
Q: The a
tag? A: 1 (text)
let p = document.createElement("p");
p.innerText = "A result!";
document.getElementById("the-place").appendChild(p);
Every DOM element object has these methods:
Name | Description |
---|---|
parentNode.appendChild(node) | places the given node at end of this node's child list |
parentNode.insertBefore(new, old) | places the given node in this node's child list just before old child |
parentNode.removeChild(node) | removes the given node from this node's child list |
node.remove() | removes the node from the page |
parentNode.replaceChild(new, old) | replaces given child with new nodes |
What happens if we don't remove the skittles when we start a new game?
Method 1: get all of the DOM elements and remove them from the DOM
function clearJar() {
let skittles = qsa(".skittle");
for (let i = 0; i < skittles.length; i++) {
skittles[i].remove();
}
}
Or ... Method 2: Set the Jar's innerHTML
to be empty!
function clearJar() {
$("jar").innerHTML = "";
}
innerHTML
hacking is badWhy not just code this way?
document.getElementById("add").innerHTML = "<p>A result!</p>";
Bad code quality (maintainability) on many levels
// Substitutes all children
$("add").innerHTML = "<p>A result!</p>";
// adds a node to the front of the list of children.
$("add").innerHTML = "<p>A result!</p>" + $("result").innerHTML;
// adds a node to the end of the list of children
$("add").innerHTML += "<p>A result!</p>";
What is the source element? A skittle (div)
What is the event we want to respond to? dblclick
What is the response? removeSkittle
What is the output/elements changed? the parent of the skittle (the jar)
//code added to addSkittle()
skittle.addEventListener("dblclick", removeSkittle);
...
// new function added (with JSDoc not seen here)
function removeSkittle() {
this.parentNode.removeChild(this);
}
Yes, you can also use remove(this);
instead of removing
from the parent node.
A user interface is like a joke. If you have to explain it, it’s not that good.
Can I, as a user, do anything "wrong" here?
But what about the guesses text box?
Solution 1: Add instructions (the user will likely not read)
Solution 2: Prevent the user from doing something they shouldn't
You can add attributes to your <input>
tags to help with validation
The Skittles code already has a attribute that restricts the input to a number
<input type="number">
We can limit the up and down arrows with min
(and max
if we choose)
<input type="number" min=0>
To insist that there is a value in the input field we can add required
<input type="number" required>
To prevent a user from being able to type in erroneous values, we can add a
regular expression to the required
attribute
<input type="number" required="\d+">
A Regular Expression is way to specify a pattern to match.
We do not go into depth on Regular expressions in this class but they are fun to learn!
Handy resources include:
input:invalid {
border: 2px dashed red;
}
input:valid {
border: 2px solid black;
}
Other HTML5 input types that can prevent a user from entering in erroneous input are listed here and include:
input
fields