lecture07
lecture07
addEventListener("click", openBox);
addEventListener("click", openBox());
Event handler syntax
What’s the difference between these two?
addEventListener("click", openBox);
addEventListener("click", openBox());
addEventListener with multiple events
removeEventListener
Event Objects!
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
Common Types of JavaScript Events
Name Description
click A pointing device (e.g. mouse) has been pressed and released on an
element
window.addEventListener('load', init);
function init() {
// TODO: Your code here
}
})();
JavaScript “strict” mode
"use strict";
… your code …
Writing "use strict"; at the very top of your JS file turns on strict syntax checking:
You should always turn on strict mode for your code in this class!
Example: strict.html
The “module pattern”
(function() {
Statements;
})();
Wraps all of your file's code in an anonymous function that is declared and immediately
called
0 global symbols will be introduced!
The variables and functions defined by your code cannot be accessed/modified externally
We'll use this pattern moving forward for all of our programs.
Example: module.html
Listening to the window “load” event
You can only access document element after the “load” event has fired
"use strict";
(function() {
window.addEventListener("load", init);
// no access to the document here
function init() {
// we now have access to the DOM tree!
// set up your initial document event handlers here.
}
})();
Example: load.html
Handy Shortcut Functions
function qsa(selector) {
return document.querySelectorAll(selector);
}
We will start using these in examples (as well as gen for document.createElement which
we'll see soon)!
A Provided JS Template
We have provided a template you can refer to for the standard JS program structure
here. (Either copy the contents of script.js from the code tab, or "fork" the repl.it
after logging in.)
You are expected to replace the example functions and comment examples with your
own (you may use the same JSDoc comments for id, qs, and qsa as is though).
DOM Manipulation: Classes
Hiding/Showing Elements
.hidden {
display: none;
}
id("my-img").style.display = "none";
.hidden {
visibility: hidden;
}
display: none:
- The element is hidden by removing it from the page entirely. It's still in the DOM, but
the page will be rendered as if it isn't.
Modifying the classList
You can manipulate the DOM element’s classList with the following methods:
Name Description
add(classname) Adds the specified class(es) to the list of classes on this element. Any that
are already in the classList are ignored.
remove(classname) Removes the specified class(es) to the list of classes from this element. Any
that are already not in the classList are ignored without an error
toggle(classname) Removes a class that is in the list, adds a class that is not in the list.
contains(classname) Returns true if the class is in the the DOM element's classList, false if not.