Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a function when Modal has loaded?

Tags:

jquery

I have the function below. I want to call the function when the myModal has finished loading. This is what I have so far.

(function ($) {


    $.ajaxSetup({"error":function(XMLHttpRequest,textStatus, errorThrown) {
          alert(textStatus);
          alert(errorThrown);
          alert(XMLHttpRequest.responseText);
      }});

    function get_fun() {

         $.getJSON('/sms/fetch/', function(json) {
            alert('json: ' + json + ' ...');
        });


    };


})(jQuery, window, document);

on page: How do I now call my function?

<script src="/js/smart.js"></script>

<script>
$(document).ready(function() {
    $('#myModal').on('shown', function() {


      get_fun()
    })
});
</script>

I get the error: ReferenceError: get_fun is not defined

like image 595
GrantU Avatar asked Dec 02 '22 23:12

GrantU


2 Answers

Found this on W3 school and works for me. Try it out. https://www.w3schools.com/bootstrap/tryit.asp?filename=trybs_ref_js_modal_event_shown&stacked=h

    $("#myModal").on('shown.bs.modal', function () {
        alert('The modal is fully shown.');
   });
like image 166
Seesi Avatar answered Jan 02 '23 19:01

Seesi


use load event to perform the task.

<script>
$(document).ready(function() {
    $('#myModal').on('load', function() {

       alert("hello!")
    })
});
</script>
like image 32
Deep Sharma Avatar answered Jan 02 '23 21:01

Deep Sharma