How to pass data attribute to ajax request on page load?

  • 0
    Hello everyone. Can you please tell me how you can pass the data attribute to ajax on page load?
    There is a script:
    function getSchedule(e){
    
      $btn = $(e.delegateTarget);
    
      if( !schedules.length || currentDoctor.id != $btn.data('guid') ){
    
        currentDoctor = doctors[$btn.data('index')];
    
        inProgress = true;
    
        $.ajax({
          type: 'GET',
          dataType: 'JSON',
          url: '/booking.php?action=getSchedule&guid=' + $btn.data('guid') + '&spec=' + currentSpecialist['id'] + '&z=1',
          error: function(){
            $modalReason.removeClass('hidden').text('error getSchedule');
          },
          success: function(response){
            if(response && response.status){
    
              schedules = response.items;
              currentView = 'list-schedules';
            } else {
              if(!response){
                $modalReason.removeClass('hidden').text('error getSchedule response');
              } else if(!response.status){
                $modalReason.removeClass('hidden').text(response.reason);
              }
            }
          },
          complete: function(){
            inProgress = false;
          }
        });
    
      } else {
    
        currentView = 'list-schedules';
    
      }
    
      renderView();
    
    }

    it fires when the button is clicked, but how to make it work when the page loads?
    Thank you.
    JavaScript Anonymous, May 28, 2020

  • 2 Answers
  • 0
    $ (document) .ready (function () {

    .........................

    });
    Anonymous

  • 0
    $(document).ready(function(){
    getSchedule();
    });
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!