问题描述:

I want to replace each div element inside all td present inside a tr element.

Using jquery .replaceWith() method or any other method

to convert them all to inputs

I tried to do that like the following but it's not working

$('tr:eq(' + RowIndex + ') td div').each()

.replaceWith("<input type='text' value=" + $('tr:eq(' + RowIndex + ') td div').val() + " />");

网友答案:
$('tr').each(function(){
  $this=$(this).find('td > div');
  $this.each(function(){
     var gettext= $(this).text();
    $(this).html('<input type="text" value="'+gettext+'"/>');
  });
});

I think this is what you want.

网友答案:
$('tr').each(function(){
var getdivtext = $(this).find('td > div').text();
$(this).find('td > div').html('<input type="text" value="'+getdivtext +'"/>');
});

hope it help

网友答案:

I used .clone() to copy the tds. Then, by using .each(), I iterate through the cloned tds, do the replacement:

var copiedRow = $('tr:eq(' + RowIndex + ') > td').clone();
copiedRow.find('div').each(function(){
    $('tr:eq(' + RowIndex + ') > td').html("<input type='text' value=" + $(this).text() + " />");
})
相关阅读:
Top