JQuery element index

I have a table full of tabular data. I need to find the index of a column (cell) in a table.

For example:

<table> <tr> <td>Column1</td> <td>Column2</td> <td>Column3</td> </tr> <tr> <td>foo</td> <td>bar</td> <td>foobar</td> </tr> </table> function TestIndexOf(someTD) { $(someTD) // what my column index? } 
+1
jquery
Nov 02 '09 at 20:24
source share
1 answer

$('td').prevAll().length will give you an index based on 0 cell

Alternatively, using index() (can pass a DOM element or jQuery object. If the object is jQuery, only the first object in the wrapped set is used)

 var cell = $('td'); // select on cell cell.parent().index(cell); 

If I remember correctly, index() will be easier to use in jQuery 1.4 and will allow you to simply call index () on an element wrapped in a jQuery object to get an index, for example

 $('td').index() // NOTE: This will not work in versions of jQuery less than 1.4 

So for your function

 function TestIndexOf(someTD) { return $(someTD).prevAll().length; } 
+5
Nov 02 '09 at 20:32
source share



All Articles