Load data dynamically as the user scrolls down using Bootstrap's Model (Lazy Load) functionality

I am trying to implement an AJAX call in a Bootstrap modal that has scrolling. The goal is to load data from the server when the user scrolls to the bottom of the modal, not the entire page. I have tried using jQuery code for this purpose, but it doesn't seem to work with the Bootstrap modal.

$(window).scroll(function() {
  if ($(window).scrollTop() == $(document).height() - $(window).height()) {
    // ajax call to retrieve and append data
  }
});

This is how my modal looks like:

<div class="modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" style="overflow-y: scroll; max-height:85%; margin-top: 50px; margin-bottom:50px;">
    <div class="modal-content">
      <div class="modal-header">
        <h3 class="modal-title">ASDFASDFASDFASDF</h3>
      </div>
      <div class="modal-body"> <!-- Long content omitted for brevity -->
      </div>
     <div class="modal-footer"> ASDFASDFASDFASDF </div>
    </div>
  </div>
</div>

My CSS for the modal:

/* Custom CSS applied after bootstrap.css */

.modal{
  display: block !important;
}
.modal-dialog{
  overflow-y: initial !important
}
.modal-body{
  height: 250px;
  overflow-y: auto;
}

A link to view the code on Bootply

Answer №1

To determine if you have reached the bottom of the modal-body while scrolling, replace references to window with modal-body in the following code:

$('.modal-body').scroll(function() {
  if($(this).scrollTop() + $(this).innerHeight() >= this.scrollHeight) {
       alert('reached bottom');
  }
});

View on Bootply

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I ensure the header and footer of a Modal stay in place while still allowing the content within the Modal to scroll?

Recently, I have been experimenting with the Modal component and have noticed that when there is long content in the Modal window, the entire modal body begins to scroll. Is there a way to make only the content scroll while keeping the header and footer ...

The YouTube video continues to play even after the container is closed

I recently worked on implementing a lightbox feature using jQuery, where a window opens upon clicking an element. Everything was working fine with images, but when I tried to open a YouTube video and play it, the video kept playing in the background even a ...

Can you identify the main component in this ReactJS code?

As a first-time poster here, I'm excited to engage with you all and learn from your expertise! Sharing my ReactJS code for feedback. Currently learning coding using Chinnathambi's book and have a specific question regarding the parent component. ...

The Angular ng-bind directive does not reflect changes in value updates

I am trying to dynamically update the content of a span element to reflect the current position of the element. The issue I am facing is that Angular does not update the value of the ng-bind attribute when I change the element's position. Here is my ...

Elevate the HTML dropdown above all other elements on the page

Currently, I am utilizing a dropdown widget from http://www.w3schools.com/ : <div class="dropdown"> <button onclick="myFunction()" class="dropbtn">Dropdown</button> <div id="myDropdown" class="dropdown-content"> <a href= ...

Retrieve the text contained within a specific element's paragraph

Is there a way to extract the text content from a <p> tag that is located within an <li> element? Sample HTML code: <ul> <li onclick="myfunction()"> <span></span> <p>This Text</p> </li> &l ...

Error: res.send is not a valid method in Node.js

I am encountering an issue with the code below, specifically receiving an error stating "res.send is not a function". I would appreciate any assistance. Code Snippet: var http = require('http'); var fs = require('fs'); var connect = ...

Utilizing component data in Vue.js to configure the router-link attribute

Is there a way to pass component data in <router-link to="...">? Here's an example: var data = { files: [{ name: "test", path: "/test" }] }; var component = { data: function() { return data; ...

consistent height across the div when expanding

There is a recurring issue I encounter where it takes too long for me to solve the problem. My goal is to ensure that the bootstrap boxes at the bottom of the page have the same height. I attempted to set a fixed height in pixels using my CSS, but this cau ...

Change the value PRIOR to executing a function with ajax included

When the page loads, the following code triggers an API request and returns the result. Afterwards, there is code that updates one of the variables when a selector is changed, and then makes the API request again using the newsFeed() function. However, I& ...

Sending JSON data back to AJAX

When I utilize AJAX to call a PHP script that returns text in JSON format, my code does not work when I specify dataType as 'json'. However, it works fine when I set it to 'html'. Here is the relevant code: $.ajax({ dataTyp ...

troubleshooting Axios errors in React applications

User/Project.js: import React, { useState, useEffect } from "react"; import Axios from "axios"; const Project = () => { const [projectName, setprojectName] = useState(""); const [projectDescription, setprojectDescriptio ...

Refresh database using ajax requests

Looking for assistance to update my database with a dropdown menu using ajax. Most examples I've seen involve retrieving data rather than updating it. Can someone please provide guidance? The code in my php updatestatus.php page is as follows: inclu ...

Retrieving information from MongoDB within a specific time range, between a designated start date

Currently, my Mongo database contains JSON data structured like this: { "uSN": "100030001", "timestamp": 1470009600, "timetag": 1082016, "monthtag": 82016, "hourtag": 11, "mintag": 10, "yeartag": 2016, "id": "d100030001_0 ...

Using Ajax with the submitHandler function in jQuery Validation Plugin

Currently, I'm working with the jQuery validation plugin in conjunction with ASP.NET MVC. I am trying to utilize $.Ajax() to submit a form, but I'm encountering an issue where the entire section of code I have written seems to be getting skipped ...

Combining multiple storageStates in a playwright context for efficient loading

I am trying to load multiple storageStates into a single context in playwright, but I am facing some issues. When using the following code: const context = await browser.newContext({ storageState: "telegram.json",storageState: "google. ...

What is the reason for having two elements positioned just 50 pixels apart, with the top element having a margin-bottom of 50px and the bottom element having a margin-top of 50px

I have encountered a strange issue while trying to ensure that two elements remain consistently 100 pixels apart in my code. Despite having no errors, the margin-bottom on the P tag is set to 50 pixels and the margin-top on a DIV below it is also set to 50 ...

Retrieve JSON data from the AJAX event's onreadystatechange function

I'm trying to retrieve a JSON object called "encuesta" from an AJAX request and store it in the global variable "jacu." Here's my code: window.onload = function() { tabla = document.getElementById("pregunta"); jencu = ajax("GET", "datos ...

Guide on showcasing active users currently online within the system utilizing CodeIgniter

I'm currently working with CodeIgniter and trying to display live online users using this framework. I attempted some code, but I'm feeling a bit lost now. I found a helpful video that I learned from: Alternatively, I followed these steps as sh ...

Creating a three-column layout with position fixed in the center

I'm struggling with achieving a maximum width of 400px for the entire set of three columns and centering them. ---------------------------------------------------- | | | |----------------------------- ...