Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

228
Views
jquery: obtener id del selector de clase

¿Hay alguna manera de que pueda obtener la identificación de un elemento de algo como:

 <a href="#" class="test" id="test_1">Some text</a> <a href="#" class="test" id="test_2">Some text</a> <a href="#" class="test" id="test_3">Some text</a>

y luego ato

$('.prueba')

Entonces, cuando hago clic en uno de los elementos, ¿recupero la identificación?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Doh... Si te entiendo bien, debería ser tan simple como:

 $('.test').click(function() { console.log(this.id); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <a href="#" class="test" id="test_1">Some text</a> <a href="#" class="test" id="test_2">Some text</a> <a href="#" class="test" id="test_3">Some text</a>

Simplemente puede acceder a la propiedad id sobre el nodo dom subyacente, dentro del controlador de eventos.

over 4 years ago · Santiago Trujillo Report

0

Use el método "attr" en jquery.

 $('.test').click(function(){ var id = $(this).attr('id'); });
over 4 years ago · Santiago Trujillo Report

0

Cuando agrega un evento de clic, this devuelve el elemento en el que se ha hecho clic. Entonces puedes usar this.id ;

 $(".test").click(function(){ alert(this.id); });

Ejemplo: http://jsfiddle.net/jonathon/rfbrp/

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!