Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

165
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda