Chetan Patel
Chetan Patel

Reputation: 183

Get attr value of first child

I have the follwing code for assigning onclick to a <tr> row:

$("#tblDepartment tr").click(function() {
       alert($(this).eq(1).attr("field"));
   });

My HTML code is here:

    <tr class="trEven" id="trDepartment4">
            <td field="ID" style="display: none;" width="50px">4</td>
            <td field="DEPT_NM" width="100%">BID DEPARTMENT</td>
    </tr>

What I would like to do is tget first child's innerHTML. How can I do that?

Upvotes: 4

Views: 9914

Answers (6)

Faizan Ali
Faizan Ali

Reputation: 509

$("#tblDepartment tr").click(function() {
   alert($(this).eq(1).attr("field#ID").html());
});

Upvotes: 1

Undefined
Undefined

Reputation: 11431

You want to do something like this

$("#tblDepartment tr").click(function() {
   alert($(this).children('td').first().html());
});

This will show you the content your looking for :)

Upvotes: 4

Elliot Nelson
Elliot Nelson

Reputation: 11557

For the sake of completeness, here's a scoped solution.

$('#tblDepartment tr').click(function () {
  alert($('td[field=ID]', this).html());
});

Upvotes: 1

Shyju
Shyju

Reputation: 218722

$(function(){
    $(".trEven").click(function() {
        alert($(this).parent().find("td:first").html());
   });    
});

Working sample http://jsfiddle.net/H2gJV/7/

Upvotes: 2

Aram Kocharyan
Aram Kocharyan

Reputation: 20421

$(this).children(":first").html();

Upvotes: 5

BenM
BenM

Reputation: 53198

$("#tblDepartment tr").click(function() {
   alert($(this).children('td').first().html());
});

Upvotes: 4

Related Questions