Select all cells in nth column with jQuery

11,226

Solution 1

There is no need to use each for this.

$('table#foo tbody td:nth-child(3)').addClass('hover');

Other than that, there's nothing wrong with your code. Problem must be somewhere else.

Solution 2

Your actual problem (not evident in the original question, but there in the fiddle) is that .index() returns a zero-based value, but :nth-child() requires a one-based value.

Share:
11,226
Muleskinner
Author by

Muleskinner

Danish Windows (vb.net) and web developer (asp.net, MySQL, javaScript) living in Granada, Spain. Working for danish HRM&D company. Latest work: http://jobmatchprofile.com http://garudabp.com/?l=enu http://www.egaruda.com

Updated on June 04, 2022

Comments

  • Muleskinner
    Muleskinner almost 2 years

    How do I select all cells in nth column of a normal html table. Ive tried this but its not working:

        $('table#foo tbody td:nth-child(3)').each(function (index) {
            $(this).addClass('hover');
        });
    

    UPDATE: Heres a jsfiddle of the unworking code: http://jsfiddle.net/Claudius/D5KMq/