Inline event handlers
**<button onclick="bgChange()">**Press me</button>
function bgChange() {
const rndCol = 'rgb(' + random(255) + ',' + random(255) + ',' + random(255) + ')';
document.body.style.backgroundColor = rndCol;
}
Event handler properties
const btn = document.querySelector('button');
**btn.onclick** = function() {
const rndCol = 'rgb(' + random(255) + ',' + random(255) + ',' + random(255) + ')';
document.body.style.backgroundColor = rndCol;
}
addEventListener()
const btn = document.querySelector('button');
function bgChange() {
const rndCol = 'rgb(' + random(255) + ',' + random(255) + ',' + random(255) + ')';
document.body.style.backgroundColor = rndCol;
}
**btn.addEventListener('click', bgChange);**
Event handler properties vs. addEventListener()
Event handler properties vs. addEventListener()
(JS) addEventListener() vs onclick()
element.classList.add(class1, class2, ...)