Home > OS >  How to offset().top from every $('.example p') element
How to offset().top from every $('.example p') element

Time:06-06

I'm trying to get every offset().top value of every $('.example p') element

console.log($('.example p')[1].offset().top)
Uncaught TypeError: $(...)[1].offset is not a function

I've tried a bunch of things, like using other selectors, using a for loop, foreach... I can only get the offset().top value from the first element of that array by removing [1], for the second, third, etc... element I can't seem to get it.

CodePudding user response:

It's seem that jQuery don't return an array of jQuery object. Check for example if $('.example p')[1] is a jQuery object. You should convert all items in jQuery object like this:

var allExample = $('.example p').map((i, e) => $(e))

And then retry:

console.log(allExample[1].offset())

CodePudding user response:

Because $('.example p')[1] return node HTML (element html)

If you want to get offset with jquery you can use : $('.example p').eq(1).offset()

  • Related