39

If a div is set as overflow:auto, how can I find out (using plain JS) if a scrollbar (Vertical / Horizontal) has been rendered?

KJ Saxena
  • 21,452
  • 24
  • 81
  • 109

1 Answers1

65

Check if clientHeight is smaller than scrollHeight.

Gh61
  • 9,222
  • 4
  • 28
  • 39
rahul
  • 184,426
  • 49
  • 232
  • 263
  • 7
    Finding every element on page that has a scrollbar `[...document.querySelectorAll('*')].filter(x => x.clientHeight < x.scrollHeight)` – A1rPun Aug 27 '19 at 08:35
  • I tried this... `function isOverflown(element) { return element.scrollHeight > element.clientHeight; } function scroll_notification() { if(isOverflown(document.getElementById("myDiv"))) { // function content } }` but I get this error... ***TypeError: null is not an object (evaluating 'element.scrollHeight')*** – G-J Jun 29 '23 at 01:00