8

What is a good way to find the index of column by it's display text?

e.g.

<table>
   <tr>
     <td>ID</td>
     <td>Name</td>
     <td>Age</td>
   </tr>
   <tr>
      ...
   </tr>
</table>

I would like to have something like

var nameIndex = getColIndex('Name'); // nameIndex = 1

Is there a quick / good way to do it? (Doesn't have to be jQuery, but would be nice)

5
  • I'd be curious to know the larger context of your question. Are you anticipating the Name to be in a different column, or will it always be in 1? Commented Mar 9, 2012 at 19:37
  • Yep, the column might move around... that's why I wanted to find it by name (which is immutable) Commented Mar 9, 2012 at 19:40
  • 1
    Will it be possible for other columns to contain a substring of Name? Or how about whitespace. Is there any chance that there could be leading or trailing whitespace, like line breaks? Commented Mar 9, 2012 at 19:41
  • Good questions. I guess it should be exact match... I like your thinking Commented Mar 9, 2012 at 19:46
  • 1
    So many things that could go wrong. ;-) If there could be leading/trailing space, then do $.trim($(this).text()) == 'Name' Or a slightly more optimized version would be $.trim($.text([this])) == 'Name' Commented Mar 9, 2012 at 20:08

3 Answers 3

22

The following both seem to work, in Chromium 17/Ubuntu 11.04:

$('tr td').filter(
    function(){
        return $(this).text() == 'Name';
    }).index();

JS Fiddle demo.

Or:

$('td:contains("Name")').index();

JS Fiddle demo.


Edited in response to OP's question, in comments, below:

but how do I limit it to the first row?

To limit it to the first row, simply use the :first selector:

$('tr:first td')

Giving:

$('tr:first td').filter(
    function(){
        return $(this).text() == 'Name';
    }).index();

JS Fiddle demo.

References:

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, but how do I limit it to the first row? this is where I got a little confused
I added this to your solution to limit to the first row... var index = $('tr:eq(0) td:contains("Name")').index(); jsfiddle.net/9uPWe
The selector would be $('tr:eq(0) td')
@trante: I'm glad to have helped! :)
2
//select the first TR element, then select its children (the TDs),
//then filter them down to only the one that contains a certain string
var theIndex = $('tr').first().children().filter(function () {
    return ($(this).text() == 'ID');
}).index();

When passing .filter() a function, if you return true for an index, then it will be kept in the selection, and if you return false then that index will be removed from the selection: http://api.jquery.com/filter

This will limit the search to the first row and give the index of the column with the specified search text (this code used ID).

Note that .index(), when used like above, will return the index of the current selection based on its sibling elements: http://api.jquery.com/index

Comments

1

http://jsfiddle.net/justiceerolin/FdhcV/

$(function(){
    $('#search').click(function(){
        $('td').each(function(index){
            if ($(this).text() == $('#lookup').val()){
                console.log(index)
            }
        });    
    });   
});​

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.