Event Types
Events are attached to DOM Nodes
Let's find a button, and set a counter
var btn = document.getElementById("counter");
var counter = 0;
And attach an eventListener
btn.addEventListener("click", function(){
counter++;
console.log("You've clicked this button "+counter+" times!");
});
A function literal looks like this:
var sayHi = function(){
console.log("hi");
}
It can be referenced without being invoked, and be invoked later
function runAnotherFunction(f) {
f();
}
runAnotherFunction(sayHi);
We can forgo the variable assignment and declare the function directly in the parameter
function runAnotherFunction(f){
f();
}
runAnotherFunction(function() {
console.log("what is happeningggggg");
});
var btn = document.getElementById("counter");
var counter = 0;
btn.addEventListener("click", function(){
counter++;
console.log(counter + " clicks!");
});
What happens when you click an element when its parent is also listening for clicks?