Thanasis Thanasis - 2 months ago 24
HTML Question

Clone a Textarea Field with its Value using Javascript

I have the code below, ready without errors. My problem is that, when you type a value in the TEXTAREA field the value does not copy itself along the clones in spite of what happens with the INPUT field. Try it your self below.

Thanks



//JAVASCRIPT PART

function insRow(row)
{
i=row.parentNode.parentNode.rowIndex;

var x=document.getElementById('myTable');
var new_row = x.rows[i].cloneNode(true);


x.rows[i].parentNode.insertBefore(new_row, x.rows[i].nextSibling);
}

<table id="myTable">
<tr>
<td><input size=10 type="text" name="myInput[]" value = ""/></td>
<td><textarea name="myTextArea[]" type="text" cols="10" rows="5" type="text" ></textarea></td>
<td><input type="button" id="addInv" value="Add" onclick="insRow(this)"/></td>
</tr>
<table>




Answer

wellll did you try adding it..

var x=document.getElementById('myTable');
var val = x.getElementsByTagName('textarea')[0].value;
var new_row = x.rows[i].cloneNode(true);
new_row.getElementsByTagName('textarea')[0].value = val;
x.rows[i].parentNode.insertBefore(new_row, x.rows[i].nextSibling);