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

305
Visualizações
How to use a javascript function alert inside a for each ajax

Here is my for each in question:

(function() {
    //for legend tags
    const editText = (text) => `<p class="d-inline"><</p><p class="d-inline">${text.replace('<','').replace('>','')}</p><p class="d-inline">></p>`;
    $.ajax({
        type: "GET",
        url: "/clinical/bbr-message-template-legend-list",
        dataType: "json",
        success: function (response){
            var tbody="";
            $.each(response.all_legends, function (key, legend) {
            tbody+='<tr>'+
                        '<td><a href="#" onclick="myFunction()">'+editText(legend.value)+'</a></td>'+
                        '<td>'+legend.text+'</td>'+
                    '</tr>';

            });
            $('#legend-list tbody').html(tbody)
        }
    });

I am trying to get an alert popping whenever I click on the tags below (blue)

1

Attempting to use a function at: <a href="#" onclick="myFunction()">

but whenever I try to use a function inside or outside ajax, my function is not being read:

function myFunction() {
  alert("I am an alert box!");
}

errors:

2

3

How do I get my function to be read?

Error in the console:

message-template:1 Uncaught ReferenceError: myFunction is not defined
    at HTMLAnchorElement.onclick
about 4 years ago · Juan Pablo Isaza
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