ale
ale

Reputation: 11820

Why is this jQuery selector not selecting ALL the cells in the nth column?

HTML:

<table id="table">
    <thead>

    </thead>
    <tbody>
        <tr>
            <td>5</td>
            <td>9</td>
        </tr>
        <tr>
            <td>3</td>
            <td>7</td>
        </tr>
    </tbody>
</table>

jQuery selector:

$('#table td:nth-child(1)')

which returns:

<td>5</td>

why doesn't it return <td>5</td> AND <td>3</td>? I want the whole nth (1st here) column.

Thanks.

Upvotes: 2

Views: 70

Answers (3)

stefanz
stefanz

Reputation: 1326

This works. For first child i recommend :first-child

$('#table tr').each(function(){
    $('td:nth-child(1)', this).css( 'color', 'red' )
});

Upvotes: 0

Adil Shaikh
Adil Shaikh

Reputation: 44740

jsFiddle

$('#table td:nth-child(1)').each(function(){
    // <td>5</td> AND <td>3</td>
});

This was just to display the element's returned by the selector

And what you did is absolutely correct $('#table td:nth-child(1)') see here

Upvotes: 2

Loamhoof
Loamhoof

Reputation: 8293

It does.

alert($('#table td:nth-child(1)').size());

So... maybe test your code? :/

Upvotes: 0

Related Questions