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

187
Visualizações
Use a function from the main file inside a AJAX loaded file

I have this code which is publishing HTML content inside #reviewsList div on page load.

<script>
    $(document).ready(function() {

         function showReviews(page = 1) {

               $.get(URL).done(function (reviews) {
                        
                        $("#reviewsList").html(reviews.html).ready(function() {
                               
                            // DECLARING SOME JS FUNCTIONS THAT ARE USED INSIDE THE LOADED FILE   

                        });

               });

         }

    });
</script>

But the problem is that I have a pagination that is working with inline onclick event - showReviews(page)

Example:
<span onclick="showReviews(3)"></span>

⬆ this is inside the AJAX loaded file

showReviews() is declared in the main file but also used inside the loaded file and I'm getting showReviews() is not a function inside the browser console when click on a pagination page span.

How can I let showReviews() be usable in the AJAX loaded file in this case?

I tried to declare the same function inside the

$("#reviewsList").html(reviews.html).ready(function() {
         
});

but without success.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The problem is because the showReviews() function is declared inside the document.ready handler, so is out of scope on an inline onclick attribute, which requires functions to be declared at global scope.

The quick fix to the problem is to move the showReviews() function declaration outside of document.ready.

The best fix is to remove the reliance on the inline event attribute and to bind your event handlers in JS. The code for that would involve changing the HTML you receive in the AJAX request to something like this:

<span data-review-page="3"></span>

Then using a delegated event handler in the JS:

jQuery($ => {
  function showReviews(page = 1) {
    $.get(URL).done(function(reviews) {
      $("#reviewsList").html(reviews.html);
    });
  }
  
  showReviews(); // on page load
  
  $('#reviewsList').on('click', 'span', e => {
    let page = $(e.target).data('review-page');
    showReviewS(page); // when a span is clicked
  }); 
});

Also note that the ready() event is redundant on anything except the document element. It can be removed from #reviewsList. You state there that you use it to declare some functions based on the new content - again, use delegated event handlers for that, not repeating the same function definitions.

about 4 years ago · Juan Pablo Isaza 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