Copy contents of one textbox to another

More efficiently it can be done as : For the one who will see the post now should use best practices of javascript.

<script>
function sync(textbox)
{
  document.getElementById('n2').value = textbox.value;
}
</script>
<input type="text" name="n1" id="n1" onkeyup="sync(this)">
<input type="text" name="n2" id="n2"/>

Well, you have two textboxes with the same ID. An Id should be unique, so you should prbably change this.

To set the value from one text box to another a simple call to getElementById() should suffice:

document.getElementById("n1").value=  document.getElementById("n2").value;

(assuming, of course you give your secodn text box an id of n2)

Tie this up to a button click to make it work.


<script>
function sync()
{
  var n1 = document.getElementById('n1');
  var n2 = document.getElementById('n2');
  n2.value = n1.value;
}
</script>
<input type="text" name="n1" id="n1" onkeyup="sync()">
<input type="text" name="n2" id="n2"/>

<html>
<script type="text/javascript">
function copy()
{
    var n1 = document.getElementById("n1");
    var n2 = document.getElementById("n2");
    n2.value = n1.value;
}
</script>
<label>First</label><input type="text" name="n1" id="n1">
<label>Second</label><input type="text" name="n2" id="n2"/>
<input type="button" value="copy" onClick="copy();" />
</html>