elena1234

Event Delegation - JavaScript ( only one eventListener )

Oct 26th, 2021 (edited)
645
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script>
  2. let liElement = document.getElementById('list');
  3. liElement.addEventListener('click', onListItemClick); // add only one eventListener on the parrent
  4.  
  5. function onListItemClick(e) {
  6. if(e.target.tagName =='Li')
  7. .........
  8. .........
  9. }
  10. </script>
  11.  
  12. or
  13.  
  14. <script>
  15. document.addEventListener("click", e => {
  16. if (e.target.matches("li")) {
  17. console.log("Hi!")
  18. }
  19. });
  20. </script>
  21.  
RAW Paste Data