2021年1月18日星期一

plain javaScript get attribute in function from event listener

So I am trying to figure out how to get the data id value from a groups of links that share the same class name. For example the links appear in this markup

<td><a class="deleteLink" href="#" data-id="4">&#10005;</a></td>  <td><a class="deleteLink" href="#" data-id="7">&#10005;</a></td>  

then I've got some javascript that creates event listeners. For some reason in the console I get an error saying "Uncaught TypeError: this.getAttribute is not a function"

function deleteUser(event){      event.preventDefault();      var userId = this.getAttribute('data-id');      if(confirm('Are you sure you want to delete this entry?')){          window.location.href = 'delete.php?id=' + userId;      }  }    var deleteLinks = document.getElementsByClassName('deleteLink');    for (i = 0; i < deleteLinks.length; i++) {      addEventListener('click',deleteUser,false);  }  
https://stackoverflow.com/questions/65784113/plain-javascript-get-attribute-in-function-from-event-listener January 19, 2021 at 08:56AM

没有评论:

发表评论