1

I have this snippet of code, testing whether or not I am getting results:

$("#texts tr").on("click", function(evt){
    var parent_id = $(("td")[0]).text();
    alert(parent_id);
});

And it delivers nothing, so I imagine I have constructed my attempt at retrieving the data improperly?

UPDATE: html (generated using jade views in Node)

extends layout
//- Format for displaying list of texts

block content
    h1 Equity Texts
    table.table.table-striped.table-hover.table-bordered
        thead
            tr
                th Objid
                th Book Title
            tbody#texts
                each text in texts
                    tr(id = "book_" + text.objid)
                        td= text.objid
                        td= text.book_title


    script(src="/jquery.min.js",type="text/javascript")
    script(src="/handler.js",type="text/javascript")
Ken Ingram
  • 1,538
  • 5
  • 27
  • 52

3 Answers3

1

As far as I understand, you want to get text of the first td of the clicked tr. Try this instead:

$("#texts tr").on("click", function(){
    var parent_id = $(this).find('td:first').text();
    alert(parent_id);
});

You can replace td:first with td:eq(0)

Mila
  • 598
  • 4
  • 9
1

The selector is t which doesn't select any elements. The code snippet ("td")[0] returns t and you are passing it as a selector to jQuery.

For getting text content of the first td element you can code:

// you probably wanted to use this syntax
$( $('td')[0] ).text();
// other alternatives
$("td").eq(0).text();
$("td:eq(0)").text();
$("td").first().text();
Ram
  • 143,282
  • 16
  • 168
  • 197
0

Just found another answer that works also. Thanks for the assist.

http://stackoverflow.com/questions/15615825/how-to-access-specific-cell-of-clicked-table-row-in-javascript

I didn't know about the cells property. If that isn't deprecated at this point.

Ken Ingram
  • 1,538
  • 5
  • 27
  • 52