Reputation: 155
I have this code
<table class="X">
<tr>
<td class="Y">Value</td>
</tr>
</table>
<table class="Z">
<tr>
<td><input class="edit-Y" type="text"></td>
</tr>
</table>
I need to get value from td with class "Y" to input with class "edit-Y" with jQuery. I tried to code scripts in a different way, but anytime i got empty field or [Object Object]. Any ideas?
Thanks!
Upvotes: 3
Views: 1247
Reputation: 5737
Try this,
$('.edit-Y').val($('.Y').html());
Additional answer for the comment
If you have html like the following and you want to select each value in td and insert it the specific input boxes,
<table class="X">
<tr>
<td class="Y1">Value1</td>
<td class="Y2">Value2</td>
<td class="Y3">Value3</td>
</tr>
</table>
<table class="Z">
<tr>
<td><input class="edit-Y1" type="text">
<input class="edit-Y2" type="text">
<input class="edit-Y3" type="text"></td>
</tr>
</table>
jquery would do like this,
$(function(){
$('td[class^=Y]').each(function(){
var tdValue = $(this).html();
var id = $(this).attr('class');
$('.edit-'+id).val(tdValue);
});
});
Upvotes: 1
Reputation: 145368
More universal approach:
$(".Z input").val(function() {
return $(".X td." + this.className.split("-").pop()).text();
});
DEMO: http://jsfiddle.net/G3tBT/
Upvotes: 1