Skip to content
Advertisement

.getAttribute() method on elements returned by querySelectorAll()

I’m trying to get the value of data attributes from multiple div elements. The elements are captured into a variable using

querySelectorAll()

I’m getting an error when I loop through the variable that contains the element and use the getAttribute() Method:

JavaScript

js:

JavaScript

Jsfiddle https://jsfiddle.net/kshatriiya/L8xsvzL1/1/

When I

console.log(divs[i])

it shows the element, I don’t know why I’m unable to use the attribute method on it.

Any pointer would be really appreciated!

Advertisement

Answer

Arrays in javascript are 0 index based

use

JavaScript

instead of

JavaScript

due to this you are getting last divs[i] as undefined and thats why console display that error

updated fiddle : https://jsfiddle.net/n3qhan4e/

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement