开发者

Copy value from one textbox to another using submit button

开发者 https://www.devze.com 2023-02-21 08:32 出处:网络
I know this can be accomplished by Javascript, and I am learning so开发者_运维百科 please tell me, when I click an update button I want the text from a textbox to be copied into another one.Assuming y

I know this can be accomplished by Javascript, and I am learning so开发者_运维百科 please tell me, when I click an update button I want the text from a textbox to be copied into another one.


Assuming you have this:

<textarea id="source"></textarea>
...
<textarea id="target"></textarea>
...
<button type="button" onclick="update();">Update</button>

Then your JS function can be:

function update() {
    document.getElementById('target').value = document.getElementById('source').value;
}


jQuery solution - check it out (jQuery that is)
$('#button').click(function(e) {
  e.preventDefault();
  $('#totextarea').val($('#fromtextarea').val());
  ...then submit the form if you wish to or whatever...
  $('#theform').submit();
});


Try the following

<script>
function onSubmitClick() {
  var box1 = document.getElementById('box1');
  var box2 = document.getElementById('box2'); 
  box2.value = box1.value;
}
</script>

<textarea id='box1'></textarea>
<textarea id='box2'></textarea>
<button onclick='onSubmitClick(); return false'>Click Me</button>

JSFiddle Demo

  • http://jsfiddle.net/Wr8L8/


<script>
function sync()
{
  // Take first and second value by element ID
  var n1 = document.getElementById('n1');
  var n2 = document.getElementById('n2');
  // Assign the value of the 1st to the 2nd text box
  n2.value = n1.value;
}
</script>

<input type="text" name="n1" id="n1" />
<input type="text" name="n2" id="n2"/>
<!-- you put a function sync to be executed on click on the button --> 
<button onclick="sync()">Synchronize</button>
0

精彩评论

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