JavaScript Events

const btn = document.querySelector('button');btn.onclick = () => console.log('It works');//This would print 'It works' to the console when the button is clicked
const btn = document.querySelector('button');btn.addEventListener('click', (e) => console.log('It works'));//This would print 'It works' to the console when the button is clicked
const btn = document.querySelector('button');btn.onclick = () => console.log('It works');
btn.onclick = () => console.log('Now what?');
//This would print 'Now what?' to the console when the button is clicked, even though we wanted both 'It works' and 'Now what?'
const btn = document.querySelector('button');btn.addEventListener('click', (e) => console.log('It works'));
btn.addEventListener('click', (e) => console.log('Now what?'));
//This would print 'It works' and 'Now what?' to the console when the button is clicked
const btn = document.querySelector('button');function pertainingToPost {
//code
btn.addEventListener('click', (e) => functionA);
}
//This would continue to functionA when the button is hit in the POSTfunction pertainingToPatchOrPut {
//code
btn.removeEventListener('click', (e) => functionA);
btn.addEventListener('click', (e) => functionB);
}
//In a PATCH/PUT this would remove the first event listener then continue to functionB when the button is hit
element.addEventListener('click', functionA, { passive: true });
element.removeEventListener('click', functionA, { passive: true });
// Succeeds
element.removeEventListener('click', functionA, { capture: false });
// Succeeds
element.removeEventListener('click', functionA, { capture: true });
// Fails
element.removeEventListener('click', functionA, { passive: false });
// Succeeds
element.removeEventListener('click', functionA, false);
// Succeeds
element.removeEventListener('click', functionA, true);
// Fails

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store