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

257
Vistas
How to get the text value of a clicked link?

I have matching text in different parts of a document. The first is a set of "tags" in a table like so:

<div id="my-div">
  <div><a href="#">tag 1</a></div>
  <div><a href="#">tag 2</a></div>
</div>

Then in several other parts of the document, I have a hidden element after the items I want to highlight when the matching link is selected like so:

<div class="hide-me">tag 1</div>

Then my click function is like this:

$('#my-div a').click(function() {
  var txt = $(this).text();
  console.log(txt);
});

The output is an empty string, but I’m not sure why.

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

your code seems to be correct, try this one too.

$('#my-div a').click(function(e) {
  var txt = $(e.target).text();
  console.log(txt);
});
over 4 years ago · Santiago Trujillo Denunciar

0

In your case I wouldn't use the text of the link, as it's possible it may change in the future (ie. you need to translate your website). The better solution is to add custom attribute to links:

<div id="my-div">
  <div><a href="#" sectionId="someId1">tag 1</a></div>
  <div><a href="#" sectionId="someId2">tag 2</a></div>
</div>

And then put the id of the hidden tag there, so you and up with:

$('#my-div a').click(function() {
  var sectionId = $(this).attr('sectionId');
  $('#' + sectionId).show();
  return false; // return false so the browser will not scroll your page
});
over 4 years ago · Santiago Trujillo Denunciar

0

$('#my-div a') is ambiguous.

It goes to read all the a tags within '#my-div'

U need to specify which of the 2 tags is clicked..

over 4 years ago · Santiago Trujillo 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