Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
170
rated 0 times [  173] [ 3]  / answers: 1 / hits: 27653  / 10 Years ago, tue, march 25, 2014, 12:00:00

I have two input fields:



<input type=text id=one name=one />
<input type=text id=two name=two />


I want to make it so that whatever that's typed into id one will automatically be put into id two.



Any ideas on how to do this? Possibly javascript needed?


More From » html

 Answers
29

Simply register an input even handler with the source textfield and copy the value to the target textfield.





window.onload = function() {
var src = document.getElementById(one),
dst = document.getElementById(two);
src.addEventListener('input', function() {
dst.value = src.value;
});
};

// jQuery implementation

$(function () {
var $src = $('#three'),
$dst = $('#four');
$src.on('input', function () {
$dst.val($src.val());
});
});

<script src=https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js>
</script>

<strong> With vanilla JavaScript</strong>
<br />
<input type=text id=one name=one />
<input type=text id=two name=two />

<br />
<br />

<strong>With jQuery</strong>
<br />
<input type=text id=three name=three />
<input type=text id=four name=four />





Fiddle


[#71784] Monday, March 24, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
monicag

Total Points: 651
Total Questions: 106
Total Answers: 104

Location: Grenada
Member since Sun, Dec 20, 2020
3 Years ago
;