最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Copy value from one textbox to another using submit button - Stack Overflow

matteradmin7PV0评论

I know this can be acplished 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.

I know this can be acplished 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.

Share Improve this question asked Apr 2, 2011 at 18:47 TotallyPwnageTotallyPwnage 531 gold badge1 silver badge6 bronze badges 1
  • can you please post what you have so far in terms of code? – KJYe.Name Commented Apr 2, 2011 at 20:32
Add a ment  | 

4 Answers 4

Reset to default 1

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/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>
Post a comment

comment list (0)

  1. No comments so far