Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery get value from one td to another

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!

like image 770
Jev Avatar asked Jan 24 '26 20:01

Jev


2 Answers

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);
  });
});
like image 105
Swarne27 Avatar answered Jan 27 '26 11:01

Swarne27


More universal approach:

$(".Z input").val(function() {
    return $(".X td." + this.className.split("-").pop()).text();
});

DEMO: http://jsfiddle.net/G3tBT/

like image 32
VisioN Avatar answered Jan 27 '26 09:01

VisioN



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!