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

129
Views
How to make function work only for one button? Bootstrap-5 scroll to modals bottom

What I want to achieve is to create two buttons: 1: that opens modal window 2: opens same modal and scrolls to bottom

code:

$('#menu-item-6706').on('click', function (){
    $('#exampleModal').on('shown.bs.modal', function (event) {
        $("#exampleModal .modal-body").animate({ scrollTop: $('#exampleModal .modal-body').prop("scrollHeight")}, 'slow');
    });
});

Here is full jsfiddle snippet: https://jsfiddle.net/mkx2auhj/1/

Script is almost working:

If you trigger for the the first time “launch demo modal” it opens modal. If you trigger “Contact” it opens modal and scrolls to bottom. So it’s correct.

The problem is if you hit “launch demo modal” again it scrolls to bottom again, which is not desirable. I want it to open standard modal like the first time.

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

0

Have a bunch of buttons that all trigger the same modal with slightly different contents? Use event.relatedTarget and HTML data-bs-* attributes to vary the contents of the modal depending on which button was clicked. https://getbootstrap.com/docs/5.1/components/modal/#varying-modal-content

Move the 'shown.bs.modal' event listener outside the click event and use event.relatedTarget to detect when the "Contact" button toggles the modal and then animate the modal accordingly. Try this

$('#exampleModal').on('shown.bs.modal', function (event) {
    if( $(event.relatedTarget).is('#menu-item-6706') ){
        $("#exampleModal .modal-body").animate({ 
            scrollTop: $('#exampleModal .modal-body').prop("scrollHeight")
        }, 'slow');
    }
});
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!