user4482202
user4482202

Reputation:

How to get value of one of <td> from several <td> using jQuery?

I have got the next code:

<tr>
    <td id='type'> Type name </td>
    <td id='number'> 102030 </td>
    <td id='software'> 1.0-Alpha </td>
</tr>

I need to get value of td with id 'number'. I use the next code right now and it works:

$('tbody tr').click(function() {
    var tdNumber = $(this).find('td:nth-child(2)').html();
});

But I don't want to find by child element, but by id. How can I do that?

Upvotes: 2

Views: 1177

Answers (3)

Sudharsan S
Sudharsan S

Reputation: 15393

you missed out closing brace in your code.

    $('tbody tr').click(function() {
        var tdNumber = $(this).find('td:nth-child(2)').html();
    });
//---^

Upvotes: 1

Arun P Johny
Arun P Johny

Reputation: 388316

Looks like you have multiple rows like this if so

<tr>
    <td class='type'> Type name </td>
    <td class='number'> 102030 </td>
    <td class='software'> 1.0-Alpha </td>
</tr>

$('tbody tr').click(function() {
    var tdNumber = $(this).find('.number').html();
})

Otherwise(ie you have only one element with the given id) then you can use the id-selector as ID of an element must be unique

var tdNumber = $('#number').html();

Upvotes: 3

AmmarCSE
AmmarCSE

Reputation: 30557

To target ids, simply use #

$('#number').html();

This will search the whole document for an element with id="number"

Upvotes: 2

Related Questions