user327712
user327712

Reputation: 3321

To grab the data in a Table's TD and display it in a Input Text box

I got a Table with some data inside it like this:

<table>
<tr>
<td>
hello world! I am new to JQuery
</td>
</tr>
</table>

<input type="text" id="displayingText" value="">

I was trying to grab the data in the TD tag of the table and display the data in the Inout Text field.
Is it possible to do it in JQuery in this case?

Upvotes: 0

Views: 896

Answers (2)

Gregoire
Gregoire

Reputation: 24832

if you have one td:

$("#displayingText").val($("td").html());

if you have several td:

$("td").each(function(){
     $("#displayingText").val($("#displayingText").val()+$(this).html());
});

Upvotes: 1

RightSaidFred
RightSaidFred

Reputation: 11327

For your code you provided, try this.

$(function() {
    $('#displayingText').val( $('table td').text() );
});

If your table changes, the answer will probably need to change too.

Here is an example.

http://jsfiddle.net/9hPG2/

Upvotes: 2

Related Questions