Zack
Zack

Reputation: 56

arrow function error when use jQuery map function

Why it can't work when i use

$('.pc-nav>li').map((x)=>$(x).text())  

to get innerText while

$('.pc-nav>li').map(function(){return $(this).text()}) 

is ok?

Upvotes: 1

Views: 105

Answers (1)

Jonas Wilms
Jonas Wilms

Reputation: 138267

Cause x is not the element, but its position (index) in the collection. Take the second argument (the actual element which you want):

$('.pc-nav>li').map((pos, el) => $(el).text())

And next time, check the docs first...

Upvotes: 1

Related Questions