Search code examples
javascriptjqueryonclickdocument-ready

how to call same either document ready or id onclick


this is a ajax call sending while page loading

 $(document).ready(function () {
      strCourseCat = $('#strCourseCat').val();
      var strengthsData = '';
      $.ajax({
      success: function (response) {},
      error: function (request, status, error) {}
      });
  });

Similarly i want to send the same call when i click on this id #opportunityTab:

$("#opportunityTab").click(function(){

});

How can i do that..?


Solution

  • Put it in a function. You may wanna move the click event and the callAjax() function in the document.ready

    $(document).ready(function () {
        strCourseCat = $('#strCourseCat').val();
        var strengthsData = '';
    
        // Ajax function
        function callAjax() {
            $.ajax({
              success: function (response) {},
              error: function (request, status, error) {}
            });
        }
    
        // Click to call ajax function
        $("#opportunityTab").click(function(){
            callAjax();
        });
    
        callAjax();
    });