1

I'm sure there is a simple explanation but oddly enough I am unable to find it anywhere, so I am here!

I am trying to get the offset().top of only the first element returned in the Jquery object. I am convinced the following code is correct but I keep getting this error:

TypeError: $(...)[0].offset is not a function

var x = $('.class')[0].offset().top;

What am I missing? Thank you in advance!

1 Answer 1

1

Accessing a jQuery object using bracket notation returns an Element object, not a jQuery object, and that has no offset() method.

To fix this your could use eq() instead:

var x = $('.class').eq(0).offset().top;

Or, in this case, first() will work too:

var x = $('.class').first.offset().top;

Also :first:

var x = $('.class:first').offset().top;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.