Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

145
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda