Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

303
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda