Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am very new to JQuery, so appologies for what may be a very simple question.

I have a table, and when i click on a row, i want the cells details to be filled into a form:

so simple example

<table id="table">
 <tr><td class="field1">1 </td><td class="field2">2 </td></tr>
</table>
<input id="input" type="text" />

So the JQuery:

$(document).ready(function() {
   $('#table tr').click(function() {
       $test = $(this).find('td').context.innerText) //problem here *
       $('#input').val( );
  • this returns the innertext of the tr (i.e "1 2"
  • How am i supose to do it...

    Thanks in advance

    Edit: ok in my rush i see i messed up what i was supose to type, here is the js i am trying:

    $(document).ready(function() {
       $('#table tr').click(function() {
           $field1 = $(this).find('td.field1').context.innerText) //problem here *
           $('#input1').val($field1);
           $field2 = $(this).find('td.field2').context.innerText) //problem here *
           $('#input12').val($field2);
    

    Appologies for the confusion

    If you want the text of each cell to be captured as a singe space-separated string to populate your input, you can do this:

    $(document).ready(function() {
       $('#table tr').click(function() {
           var $test = $(this).find('td').map(function() {
               return $(this).text();
           }).get().join(" ");
           $('#input').val($test);
    

    EDIT just call text(), e.g.:

    var $field1 = $(this).find('td.field1').text();
                    I am not sure if my edits made any changes to your suggestion, if you could have a look. And thank you for your reply, i will have a look
    – Andy
                    Mar 8, 2010 at 12:30
        $("tr#tr_id").click(function(){
            $("#hiddenDiv").show();
            $("#leadID").val($(this).find("td.l_id").text());
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.