Aradhana Lohan
Aradhana Lohan

Reputation: 35

value of td using jquery but from table inside table

I want to get td value of element inside td in table with class t2 how can i achieve it?? i tried this but didnt help

var test = $(.lv1 tr:nth-child(" + row2 + ")").children("td:last").children(".lv2 tr:last").children("td:last").html();

table

<listview1>
<itemtemplate>
    <table class="t1">
    <tr class="lv1"><td></td><td></td></tr>
    <tr class="lv1"><td colspan="2">
<listview2>
<itemtemplate>
    <table class="t2">
    <tr class="lv2">
    <td></td>
    </tr>
    <tr class="lv2">
    <td></td>
    </tr>
    </table>
</itemtemplate>
</listview2>
    </td></tr>
    </table>
</itemtemplate>
</listview1>

Upvotes: 2

Views: 1292

Answers (3)

Aradhana Lohan
Aradhana Lohan

Reputation: 35

Thanks mrtsherman I divided two functions td click and on textbox submit and kept the global variable. Now its working fine

var row2;
var test;

$("td").live('click', function () {
    var row = $(this).parent().parent().children().index($(this).parent());
    row2 = row + 1;
    test = $(this).closest("#Table1 tr:nth-child(" + row2 + ") ")
                     .find('table.gh tr:first td:first').html();
    // alert(test);
    var test2 = $(this).closest("#Table1 tr:nth-child(" + row2 + ") ")
                     .find('table.gh tr:last');
                });
    $(".commenttext").keyup(function (event) {
        if (event.which == 13) {
           var test2 = $("#ListView1_Table2 tr:nth-child(" + row2 + ") ")
                         .find('table.gh tr:last');
           test2.after('id:'+ test);
           $(".commenttext").val("");
           $(".commenttext").blur();
           $(".commenttext").height('20px');
        }
    });

Upvotes: 0

mrtsherman
mrtsherman

Reputation: 39872

Okay, based on your feedback this is what I think you are after. Click on the go button to see it in action.

http://jsfiddle.net/mSHKC/

$('#go').click( function() {
    var id = 'foo'; //hardcode id retrieval on button press
    //value of first td of first row in table.t2
    var firstTdFirstRow = $(this).closest('table.t1').find('table.t2 tr:first td:first').html();
    //value of first td of last row in table.t2
    var firstTdLastRow =  $(this).closest('table.t1').find('table.t2 tr:last td:first').html();
});

You had this comment, but I'm not sure what it means because I think you made a spelling error.

 its selecting right td which i want to but while appenind its getting error

Upvotes: 2

Adam Rackis
Adam Rackis

Reputation: 83358

Why not use something more simple like:

$("tr.lv2:eq(0) td").text();

To get the first such cell

$("tr.lv2:eq(1) td").text();

For the second, and so on

Or if you have the index as a variable, you can use the eq function:

$("tr.lv2").eq(index).find("td").text();

Upvotes: 1

Related Questions