cantaffordavan
cantaffordavan

Reputation: 1447

Jquery to copy values from table td to input

I have a table that displays all my clients information. I want to be able to take the clientid column and copy all the id's into a input box. The TD has a unique ID which is 'clientid'. If I try to copy the text to the input it just gets the first value in the table not all of them.

<table>
  <tr>
    <td id="clientid">
       45
    </td>
    <td>
       John Salsasauce
    </td>
  </tr>
  <tr>
    <td id="clientid">
       53
    </td>
    <td>
       Andrew Foobaloba
    </td>
  </tr>
// more here...
</table>

This is the jQuery I am using but it only gets the first instance of #clientid value.

$("button#copybutton").click(function(){
   $("textarea#copy").val($("#clientid").text()); 
});

Upvotes: 1

Views: 1443

Answers (1)

John
John

Reputation: 86

Use class (say myclass)

   $("textarea#copy").val( $(".myclass").map(function(){
      return $(this).text();
    }).get().join(", ") );

Upvotes: 4

Related Questions