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

209
Views
Get the index of columns of table if the column text found in different tags

How can we find the index of columns satisfying below tables ? Some text are in <th> directly, some are in <a> tag and some are in <div>

      <thead>
       <tr>
          <th>Text 1</th>
          <th>Text 2</th>
          <th>Text 3</th>
        </tr>
        
    </thead>    
//Another table:
    <thead>
       <tr>
           <th>
            <a>Text 1</a>
            </th>
            <th>
            <a>Text 2</a>
            </th>
        </tr>
    </thead>

 //Another table:       
    <thead>
       <tr>
           <th>
            <div>Text 1</div>
            </th>
            <th>
            <div>Text 2</div>
            </th>
        </tr>
    </thead>

I have tried below, but this is not complete yet, how can we handle all in single line of code ?

let columnName = "Text 1" ;
const rowText1 = $('#tableID').find('thead').find(`tr:has(a:contains(${columnName}))`).index();
const rowText2 = $('#tableID').find('thead').find(`tr:has(th:contains(${columnName}))`).index();

enter image description here

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

you can use $('#tableID thead tr th:contains('+columnName+')').index(); it will return the index you are looking for.

Instead of .find() use jQuery descendant selector that find your tag under all decendents.

here's the JSFiddle.

Notice I have switched column Text 2 in each table and it returns the correct index.

about 4 years ago · Juan Pablo Isaza Report

0

You can omit the selector to search everything with contains:

$(() => {
  const rowText1 = $('#tableID').find('tr:has(:contains("Text 2"))').index();
  console.log(rowText1) // Index would be 1
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="tableID">
  <thead>
       <tr></tr>
       <tr>
          <th>Text 1</th>
          <th>Text 2</th>
          <th>Text 3</th>
        </tr>
    </thead>    
</table>

about 4 years ago · Juan Pablo Isaza Report

0

Consider the following example.

$(function() {
  var needle = "Text 1";
  var stack = [];
  $("table > thead > tr").each(function(i, el) {
    $("th", el).each(function(j, cell) {
      if ($(cell).text().trim().indexOf(needle) > -1) {
        stack.push({
          table: i,
          col: j
        });
      }
    });
  });
  console.log("Needles Found", stack);
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <thead>
    <tr>
      <th>Text 1</th>
      <th>Text 2</th>
      <th>Text 3</th>
    </tr>
  </thead>
</table>
<table>
  <thead>
    <tr>
      <th>
        <a>Text 1</a>
      </th>
      <th>
        <a>Text 2</a>
      </th>
    </tr>
  </thead>
</table>
<table>
  <thead>
    <tr>
      <th>
        <div>Text 1</div>
      </th>
      <th>
        <div>Text 2</div>
      </th>
    </tr>
  </thead>
</table>

Iterating each table, and each header cell, we can seek out the table and column.

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!