We used the <!DOCTYPE html>2 method to add a handler for the <!DOCTYPE html>0 event to the <!DOCTYPE html>4 element. This event handler will be called whenever the user clicks the box. In the handler function, we called the <!DOCTYPE html>1 method on the element to remove it from the DOM. We could also have used the <!DOCTYPE html>6 property on the <!DOCTYPE html>7 object passed to the handler to remove the element. const box = document.getElementById('box');box.addEventListener('click', (event) => { We can use the <!DOCTYPE html>7 object to access useful information and perform certain actions related to the event. For the <!DOCTYPE html>0 event, the <!DOCTYPE html>6 property lets us access the DOM element that was clicked. Removing the element with the <!DOCTYPE html>6 property is useful when we want to dynamically remove many elements onclick. For example: index.html <!DOCTYPE html> const box = document.getElementById('box');box.addEventListener('click', () => {2 .container { index.js const boxes = document.getElementsByClassName('box');for (const box of boxes) { </body> const box = document.getElementById('box');box.addEventListener('click', () => {5 property, which returns the element that the event listener was added to. Originally published at codingbeautydev.com 11 Amazing New JavaScript Features in ES13This guide will bring you up to speed with all the latest features added in ECMAScript 13. These powerful new features will modernize your JavaScript with shorter and more expressive code. Sign up and receive a free copy immediately. More content at PlainEnglish.io. Sign up for our free weekly newsletter. Follow us on Twitter, LinkedIn, YouTube, and Discord. |