I often find tutorials on this topic that are either false or assume that you want to detect the whole page scrolling. Usually that's not the case and you only want to detect a div's scroll position.

Create a div with overflow-x: auto; and a fixed height (not height: auto;). Now put this code in your $(document).ready() segment and it should work.

Note: Every time you update the div's contents you must run this code again, it doesn't work with $(element).on('scroll').