Erisan Olasheni
Erisan Olasheni

Reputation: 11

jQuery could not get the scroll height of a div element

I was trying to get the height of an element with jQuery but it was returning undefined

Code: alert($('.forIndex').scrollHeight()) Please any help will be greatly appreciated at: http://www.tulzmasterz.com/tutorials/

Upvotes: 0

Views: 367

Answers (2)

sonara
sonara

Reputation: 41

To get the scrollHeight of a jQuery element you will need to select the first element from the query.

Example:

alert($('.forIndex')[0].scrollHeight())

(notice the "[0]")

Source: https://stackoverflow.com/a/25610045/969869

Upvotes: 0

taxicala
taxicala

Reputation: 21759

If you wan't to get the height as you stated in your question, use height:

alert($('.forIndex').height())

In order to use scrollHeight, the element should have indeed a scroll, by setting it's css attribute overflow to scroll, or auto (and the height of the element should be shorter than the content inside, so that the content generates a scroll.

Upvotes: 2

Related Questions