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

147
Vistas
How to get the id of a clicked table header item with jQuery

Question

When I click the table header row, I would like a generic function to get the id of the clicked <th> element, so I don't have to create a separate function for each header item.

I'm using jQuery, and have the following code:

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 Respuestas
Responde la pregunta

0

if you include the th to the selector then you can access the id using $(this)

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

0

With plain Vanilla JS

You can call querySelectorAll() on all th elements and use a loop to iterate and console.log() attributes once the EventListener is triggered by the element clicked.

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 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