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

121
Vistas
How can I add ajax get call to a html element?

So I am trying to run a js code that turns all of <a></a> elements under another element <b></b> (the actual name is unique in the HTML) to a link that makes an HTTP get request. This is what I got and it doesn't do anything.

$("b a").each(function(index, element) {
  var data = element.getAttribute("data-something");
  // $.ajax("https://example.com/" + data);
  console.log(data);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<b data-something="an-id" class="missing">has has not been installed. to see more click on<a>Link</a></b>

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

First, the data- attribute is not on the a element, but on the b element. So on link click, you need to get the data from the parent.

Second, you need an event handler on the link.

$("b a").each(function(index, element) {
  let data = element.closest("b").getAttribute("data-something");

  $(element).on("click", function() {
    console.log(data)

    // Commented out for this snippet only
    //$.ajax("https://example.com/" + data);
  })
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<b data-something="an-id" class=\ "missing\">has has not been installed. to see more click on this <a>Link</a></b><br>

about 4 years ago · Juan Pablo Isaza Denunciar

0

The scenario is not clear much.

but you can do it with the below code

 $("bg>a").each(function (index, element) {
           var data = element.getAttribute("data-something");
           $.get(("https://example.com/id?" + data);
       });
about 4 years ago · Juan Pablo Isaza Denunciar
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