0

I need to style an entire column in a table. The column should correspond to the element with id="current" in the thead row.

<table>
<thead>
    <tr>
        <td></td>
        <td></td>
        <td><div id="current"></div></td>
        <td></td>
    </tr>
</thead>
<tbody>
    <tr>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
    </tr>
    <tr>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
    </tr>
</tbody>
</table>

Now, I can add a class to the correct column, I just need to figure out a way to tell the column count number.

var num = ???
$('td:nth-child(num)').addClass('selectedCol');
1
  • 1
    you want to use a variable.. not a string so you need to do $('td:nth-child(' + num + ')')
    – wirey00
    Oct 4, 2012 at 18:53

1 Answer 1

5

try this

$("#current").closest('td').index()

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.