Inline Events
Lecture 259 goes into some detail about the onclick and ondblclick events, which are very rarely implemented inline using HTML.
Lecture 261 is about the onclick property. We create a button (along with a unique JS script) which calls a function. The function will print a humorous message to the console (although you will need to inspect the page to see it). We're the onclick property for the message, and just look at how we use the onmouseenter property too!
addEventListener
This is a generic method that allows us to call a specific preset function whenever a specified event (such as a click, dblclick, scroll, or keydown) is 'delivered to the target'.
addEventListener(type, listener)
addEventListener(type, listener, options)
addEventListener(type, listener, useCapture)
const btn3 = document.querySelector('#v3');
btn3.addEventListener('click', function () {
alert("CLICKED!");
})
function twist(){
console.log("TWIST!");
}
function shout(){
console.log("SHOUT");
}
//If we do this then the LAST one ALONE is called...i.e. only shout will display
//tasButton.onclick = twist;
//tasButton.onclick = shout;
//With addEventListener(), BOTH are called!
const tasButton = document.querySelector('#tas');
tasButton.addEventListener('click',twist);
tasButton.addEventListener('click',shout);