0

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/

2 Answers2

2

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.

taxicala
  • 21,408
  • 7
  • 37
  • 66
  • 1
    Absolutely, and just thought I'd add that if your element has padding or something else increasing its height you can use .outerHeight() instead of .height() to get its total, calculated height. – Jonathan Bowman Oct 22 '15 at 15:26
0

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

Community
  • 1
  • 1
sonara
  • 41
  • 3