jQuery – Detect if an element is scrolled till the end
This post shows you the way to detect if an HTML element is scrolled till the end using jQuery. We often apply this to the page which contains License Agreement section and we only want to enable the Submit button if the user scrolls the license section till the end.
Here is the full example, let’s create example.html
file with the following content. You can also click on the “Run Example” button to see how it works.
example.htmlRun Example
<html>
<head>
<title>Detect If an Element is scrolled till the end | ByteNota.com</title>
<style type="text/css">
#license-agreement {
width: 200px;
height: 200px;
overflow: auto;
}
</style>
</head>
<body>
<div id="license-agreement">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</div>
<button id="submit-button" disabled="disabled">Submit</button>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script>
$(function() {
$('#license-agreement').scroll(function() {
var licenseElement = $(this).get(0);
if ((licenseElement.scrollTop + licenseElement.offsetHeight) >= licenseElement.scrollHeight) {
// the license is scrolled till the end
$('#submit-button').attr('disabled', false);
}
});
});
</script>
<br><br><div><a href="https://bytenota.com/jquery-detect-if-an-element-is-scrolled-till-the-end/" target="_blank" rel="noopener">← Back to the post.</a></div>
</body>
</html>
In the above code, we add the HTML license agreement section, a submit button disabled by the default, include jQuery library, and the jQuery code that we enable the submit button if the license section is scrolled till the end as the code above in the callback function of jQuery scroll()
method.