frenchbaguette
frenchbaguette

Reputation: 1339

Grabbing selector value from a tag

In short:

This code:

for(let i = 0; i <= items.length; i++){
    console.log(items[i])
}

Returns me this data:

<a class="photo ajax2" target="_blank" href="/profile/show/3209135.html" data-first="1" data-next="3206884">
<a class="photo ajax2" target="_blank" href="/profile/show/3206884.html"  data-next="3209135">
<a class="photo ajax2" target="_blank" href="/profile/show/3209135.html" data-next="3209755">

I want to grab the data-next values from all links. How can I do that ? I've tried this:

for(let i = 0; i <= items.length; i++){
    console.log(items[i].querySelector('a["data-next"]'));
}

but this doesn't work. I need this only with vanilla JS.

ANSWERED

Upvotes: 0

Views: 28

Answers (1)

Mamun
Mamun

Reputation: 68933

items[i] refers to the element itself, thus querySelector() on that will return nothing. Try with getAttribute():

console.log(items[i].getAttribute("data-next"));

Upvotes: 1

Related Questions