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

305
Views
obtener el valor de un atributo del elemento en el que se hizo clic
<ul id='langs'> <li data-val='en'>english</li> <li data-val='fr'>francais</li> <li data-val='it'>italiano</li> </ul>

cuando el usuario hace clic en cualquiera de estos <li> quiero alert() es el valor del atributo data-val

alguien sabe como?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Respuesta original - 2011

 $('li').click(function () { alert($(this).data('val')); });

Ver DEMO .

Actualización - 2017

Tenga en cuenta que si desea usar la sintaxis de la función de flecha ES6, no puede usar this y necesita usar e.currentTarget en su lugar, donde e es el objeto de evento pasado como el primer parámetro al controlador de eventos:

 $('li').click(e => alert($(e.currentTarget).data('val')));

Ver DEMO .

over 4 years ago · Santiago Trujillo Report

0

Esto debería hacer el trabajo:

 $(document).ready(function() { $('#langs li').click(function() { alert($(this).attr('data-val')); }); });

Eche un vistazo a los documentos

over 4 years ago · Santiago Trujillo Report

0

$('li').click(function() { alert($(this).attr('data-val')); });
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!