开发者

Copying data from one text box to another using Java Script

I want to copy data from one text box to another in html automatically ie., as I edit the fi开发者_如何学Gorst text box the second one should reflect the same spontaneously


call javascript function on onkeypresss

function copy_data(val){
 var a = document.getElementById(val.id).value
 document.getElementById("copy_to").value=a
}

EDITED USE onkeyup or onblur instead

<html>
<head>
    <script>
    function copy_data(val){
     var a = document.getElementById(val.id).value
     document.getElementById("copy_to").value=a
    }    
    </script>
</head>
<body>
<input type="text" name ="a" id="copy_from" onkeyup="copy_data(this)"/>
<input type="text" name ="a" id="copy_to"/>
</body>
</html>


In jQuery, something like this:

$("#txtBox1").keypress(function() {
  $("#txtBox2").val($(this).val());
}


You do easily with JQuery:

<input type="text" id="box1" />
<input type="text" id="box2" />

<script type="text/javascript">
$(function(){
  $("#box1").keypress(function()
  {
    $("#box2").val($(this).val());
  }
});
</script>


You can achieve by this...

function FillBilling(f) {
  if(f.billingtoo.checked == true) {
    f.billingname.value = f.shippingname.value;
    f.billingcity.value = f.shippingcity.value;
  }
}
To add more fields, just add to the parameters shown above...like this:
    f.billingstate.value = f.shippingstate.value;
    f.billingzip.value = f.shippingzip.value;
The HTML for the form you will use looks like this:
<b>Mailing Address</b>
<br><br>
<form>
Name:
<input type="text" name="shippingname">
<br>
City:
<input type="text" name="shippingcity">
<br>
<input type="checkbox" name="billingtoo" onclick="FillBilling(this.form)">
<em>Check this box if Billing Address and Mailing Address are the same.</em>
<P>
<b>Billing Address</b>
<br><br>
Name:
<input type="text" name="billingname">
<br>
City:
<input type="text" name="billingcity">
</form>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜