Javascript click event listener on multiple elements and get target 'rel' attribute

StackOverflow https://stackoverflow.com/questions/20114350

  •  03-08-2022
  •  | 
  •  

Вопрос

Code:

var articles = document.getElementsByTagName('li');
for (var i = 0; i < articles.length; i++) {
    articles[i].addEventListener('click',redirect,false);
}
function redirect(){
    alert(this.rel);
}

This code works for attribute id but not for attribute rel. Can anyone help me to get rel attribute?

Это было полезно?

Решение

You should be accessing these attributes with element.getAttribute('attribute_name')

alert(this.getAttribute('rel'));

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top