Continue with JavaScript Events
Introduce some new DOM manipulation with JS
HW1 late submissions still open
CP2 released now, go play around with JS!
Quiz grades will be released soon. Will discuss the answers briefly on Friday.
What is the difference between a node and an element?
We can use JavaScript to attach functions to elements when an event (e.g. "click") occurs.
To do so, we need to:
event
to trigger a responseOnce we have this information, we can use addEventListener
to hook everything up!
What's the difference?
addEventListener("click", openBox);
vs.
addEventListener("click", openBox());
addEventListener("click", openBox());
Recall that the event handler function can be attached to objects (window, DOM elements, etc.)
source.addEventListener("click", responseFunction);
function responseFunction(e) {
// we can access the click Event object here!
}
When the event occurs, an Event object is created and passed to the event listener. You can "catch" this event object as an optional parameter to get more information about the event.
Example: event-objects.html
Name | Description |
---|---|
load | Webpage has finished loading the document |
scroll | User has scrolled up or down the page |
click | A pointing device (e.g. mouse) has been pressed and released on an element |
dblclick | A pointing device button is clicked twice on the same element |
keydown | Any key is pressed down |
keyup | Any key is released |
mouseenter | A pointing device is moved onto an element that has the attached listenere |
mouseover | A pointing device is moved onto the element that has the listener attached to itself or one of its children |
mousemove | A pointing device is moved over an element |
mousedown | A pointing device button is pressed on an element |
mouseup | A pointing device button is released over an element |
You can find a full list here
Name | Description |
---|---|
document.createElement("tag") | creates and returns a new empty DOM node representing an element of that type |
// create a new <h2> node
let newHeading = document.createElement("h2");
newHeading.textContent = "This is a new heading!";
Note: Merely creating an element does not add it to the page
You must add the new element as a child of an existing element on the page...
When you have a parent DOM node, you can add or remove a child DOM node using the following functions:
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.replaceChild(new, old) | replaces given child with new nodes |
parentElement.insertAdjacentElement(location, new) | inserts new element at given location relative to the parent |
let li = document.createElement("li");
li.textContent = "A list item!";
id("my-list").appendChild(li);
When you have a parent DOM node, you can remove a child DOM node using the following functions:
Name | Description |
---|---|
parentNode.removeChild(node) | removes the given node from this node's child list |
node.remove() | removes the node from the page |
qs("#my-list li:last-child").remove();
/* or */
let li = qs("#my-list li:last-child");
li.parentElement.removeChild(li);
One more alias function!
When creating new DOM elements using JS, you may use document.createElement
often.
We have added one more alias function, gen
to include with id
, qs
, and qsa
.
function gen(tagName) {
return document.createElement(tagName);
}
Literal HTML: Ed lesson
When you have a parent DOM node, you can add or remove a child DOM node using the following functions:
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.replaceChild(new, node) | replaces given child with new node |
parentElement.insertAdjacentElement(location, element) | inserts element at given location relative to the parent |
Look familiar?
We can use the DOM tree to traverse parent/children/sibling relationships (e.g. to remove an element from its parent node)
Every node's DOM object has the following (read-only) properties to access other DOM nodes in the tree:
Name(s) | Description |
---|---|
firstElementChild, lastElementChild | start/end of this node's list of children elements |
children | array of all of this node's children (not the same as childNodes , which includes text) |
nextElementSibling, previousElementSibling | neighboring element nodes with the same parent, skipping whitespace nodes |
parentNode (parentElement) | the element that contains this node |
These are common traversal properties we'll see, but you can find a complete list here.
All objects in the DOM are Nodes, but not all Nodes are Elements (of the HTML variety).
An example:
Call document.getElementById("example").children
and document.getElementById("example").childNodes
on me!
Scrambled: Ed lesson
<div id="container">
<div id="column1">
<div>1</div>
<div id="box2">2</div>
<div>3</div>
</div>
<div id="column2">
<div>4</div>
<div>5</div>
<div>6</div>
</div>
</div>
id("container").children; // [#column1, #column2]
id("column1").firstElementChild;
id("container").firstElementChild.firstElementChild;
id("box2").previousElementSibling;
// all three ways to get <div>1</div>
id("box2").nextElementSibling; // <div>3</div>
id("column2").lastElementChild; // <div>6</div>
id("box2").parentNode.parentNode; // #container
Especially if you're watching a recording, write your question on PollEverywhere and I'll answer them at the start of next lecture.
Either on your phone or computer, go to PollEv.com/robcse