Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

143
Views
How to use Lightbox on AJAX loaded content

I am using Boostrap 5 and this Lightbox library https://trvswgnr.github.io/bs5-lightbox/

It works fine on pages loaded normally but it will not load on AJAX loaded content.

I have tried this code to make it work with AJAX but it did not work.

$(document).on('click', '[data-toggle="lightbox"]', function(event) {
    event.preventDefault();
    $(this).Lightbox();
});

It returned this error:

Uncaught TypeError: $(...).Lightbox is not a function

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If you're using the vanilla JS version, the script only adds event handlers to elements with the data-toggle="lightbox" attribute that are present when the script loads, hence new elements loaded via AJAX calls not working.

If you're able to use Node (or hack around not using node with a module loader like RequireJS), you can instantiate a new Lightbox when each new element loads, per the docs:

$(document).on('click', '[data-toggle="foo"]', function (event) {
    event.preventDefault();
    const el = $(this);
    const lightbox = new Lightbox(el);
    lightbox.show();
});

Note that the correct syntax here is to create a new lightbox from the element and then use lightbox.show()... $(this).Lightbox() will never work.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!