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

144
Views
Cómo obtener la identificación de un elemento de encabezado de tabla en el que se hizo clic con jQuery

Pregunta

Cuando hago clic en la fila del encabezado de la tabla, me gustaría una función genérica para obtener la id del elemento <th> en el que se hizo clic, por lo que no tengo que crear una función separada para cada elemento del encabezado.

Estoy usando jQuery y tengo el siguiente código:

HTML

 <div id="title"> <table class="policies sortable"> <thead id="h-th"> <tr id="h-tr"> <th width="3%" id="h-client">Client</th> <th width="7%" id="h-company-code">Company Code</th> <th width="9%" id="h-unique-reference">Unique Reference</th> <th width="8%" id="h-doc-date">Document Date</th> <th width="8%" id="h-post-date">Posting Date</th> <th width="7%" id="h-doc-type">Document Type</th> <th width="5%" id="h-trip-id">Trip Id</th> <th width="7%" id="h-ticket-no">Ticket Number</th> <th width="6%" id="h-calc-tax">Calculate Tax</th> <th width="15%" id="h-passenger-name">Passenger Name</th> <th width="4%" id="h-error">Valid</th> <th width="4%" id="h-processed">Processed</th> <th width="4%" id="h-payment">Payment</th> <th width="11%" id="h-updated">Updated</th> </tr> </thead> </table> </div>

JavaScript

 $("#h-tr").click(function(item) { console.log(item); // get the id of the clicked <th> });
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

si incluye el th en el selector, puede acceder a la identificación usando $(this)

 $("#h-tr th").click(function(item) { console.log($(this).prop('id')); });
about 4 years ago · Juan Pablo Isaza Report

0

Con vainilla simple JS

Puede llamar a querySelectorAll() en todos th elementos y usar un bucle para iterar y los atributos de console.log console.log() una vez que el elemento en el que se hizo clic activa EventListener.

 document.querySelectorAll('th').forEach(element => { element.addEventListener('click', () => { console.log(element.getAttribute('id')) }) })
 th{cursor:pointer}
 <div id="title"> <table class="policies sortable"> <thead id="h-th"> <tr id="h-tr"> <th width="3%" id="h-client">Client</th> <th width="7%" id="h-company-code">Company Code</th> <th width="9%" id="h-unique-reference">Unique Reference</th> <th width="8%" id="h-doc-date">Document Date</th> <th width="8%" id="h-post-date">Posting Date</th> <th width="7%" id="h-doc-type">Document Type</th> <th width="5%" id="h-trip-id">Trip Id</th> <th width="7%" id="h-ticket-no">Ticket Number</th> <th width="6%" id="h-calc-tax">Calculate Tax</th> <th width="15%" id="h-passenger-name">Passenger Name</th> <th width="4%" id="h-error">Valid</th> <th width="4%" id="h-processed">Processed</th> <th width="4%" id="h-payment">Payment</th> <th width="11%" id="h-updated">Updated</th> </tr> </thead> </table> </div>

about 4 years ago · Juan Pablo Isaza 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!