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

164
Views
Call ajax on page load and on click to update data

I am trying to have my ajax load the data once the page loads and be able to click on a button to update the data when need it. Right now it only works on the button click. Here is a sample of what I have.

<a id="update" href="#" class="button">Update Data</a>
<div id="container">
  <div id="mydata"></div>
</div>
$(document).ready(function() {
  $("#update").click(function(e) {
    e.preventDefault();

    $.ajax({
      url: "mycode",
      dataType: "json",
      data: {
        //id: $(this).val(),
      },
      success: function(result) {
        alert(JSON.stringify(result));
      },
      error: function(result) {
        console.log(result);
      }
    });
  });
});

Thanks for looking!

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

To achieve this you can either leave your code as is and just raise a click event on load:

$("#update").click(function(e) {
  e.preventDefault();
  // ajax logic here...
}).click(); // < raise event on load

Alternatively you can extract the AJAX logic to a function which is called in both places:

function makeAjaxCall() {
  // ajax logic here...
}

$('#update').click(function(e) {
  e.preventDefault();
  makeAjaxCall();
}); // on click

makeAjaxCall(); // on load
over 4 years ago · Santiago Trujillo Report

0

You need to put your AJAX call in your .ready scope, it's currently blocked by the click function.

$(document).ready(function() {

  updatePage();

  $("#update").click(function(e) {
     e.preventDefault();

     updatePage();
  });


 });

function updatePage(){
    $.ajax({
         url: "mycode",
    dataType: "json",
         data: { 
             //id: $(this).val(),
         },
         success: function(result) {

        alert(JSON.stringify(result));

         },
         error: function(result) {
        console.log(result);
         }

     });
}
over 4 years ago · Santiago Trujillo Report

0

Here you go:

$(document).ready(function() {
    $("#update").click(function(e) {
        e.preventDefault();
        loadData();
      }
      loadData();
    });

  function loadData() {
    $.ajax({
        url: "mycode",
        dataType: "json",
        data: {
          //id: $(this).val(),
        },
        success: function(result) {

          alert(JSON.stringify(result));

        },
        error: function(result) {
          console.log(result);

        });
    });

}
<a id="update" href="#" class="button">Update Data</a>

<div id="container">
  <div id="mydata"></div>
</div>

over 4 years ago · Santiago Trujillo 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!