How can I copy the content of a textbox to another textbox using javascript?

I have two textboxes t1

and t2

an html page.

I would like to copy the content t1

to t2

using javascript on every key press t1

.

+2


source to share


3 answers


Ol 'since you added the "beginner" tag:

<input type="text" id="t1" onkeyup="document.getElementById('t2').value = this.value" />
<input type="text" id="t2" />

      



A more robust solution also uses an event change

(for those who insert with the right mouse button) and does not inline the event handler logic:

<input type="text" id="t1" />
<input type="text" id="t2" />

<script>
var t1 = document.getElementById('t1');
t1.onkeyup = t1.onchange = function() {
    document.getElementById('t2').value = this.value;
};
</script>

      

+6


source


Using jquery would look something like this:



$(function() { $(t1).keyup(function() { $(t2).val($(t1).val()) } })

+3


source


You can use this ugly thing:

<html>
<body>
  <textarea id="t1" onkeyup="document.getElementById("t2").value = this.value;"></textarea>
  <textarea id="t2"></textarea>
</body>
</html>

      

+1


source







All Articles