Advertisement
trentjs

event listener by class, get data attributes - js only

Nov 3rd, 2020 (edited)
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // HTML SOURCE DIVS...
  2. //<div id="div00" class="classTarget" data-info="Div Zero Info">Div Zero</div>
  3. //<div id="div01" class="classTarget" data-info="Div One Info">Div One</div>
  4.  
  5. // GET CLASS AS ARRAY...
  6. var targetClickList = document.querySelectorAll(".classTarget");
  7. // ADD EVENT LISTENER...
  8. for(var i = 0; i < targetClickList.length; i++) {
  9.     targetClickList[i].addEventListener("click", doTargetClick);
  10. }
  11.  
  12. // EVENT LISTENER FUNCTION...
  13. function doTargetClick(event){
  14.     var thisId = event.target.id;
  15.     var thisInfo = event.target.getAttribute('data-info')
  16.     console.log(thisId + " : " + thisInfo);
  17. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement