Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
141
rated 0 times [  148] [ 7]  / answers: 1 / hits: 35562  / 9 Years ago, thu, february 5, 2015, 12:00:00

I need to copy the text entered in a field (whether it was typed in, pasted or from browser auto-filler) and paste it in another field either at the same time or as soon as the user changes to another field.



If the user deletes the text in field_1, it should also get automatically deleted in field_2.



I've tried this but it doesn't work:



<script type=text/javascript>
$(document).ready(function () {

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


Any ideas?


More From » jquery

 Answers
8

You are almost there... The function is correct, you just have to assign it to the change event of the input:



<script type=text/javascript>
$(document).ready(function () {

function onchange() {
//Since you have JQuery, why aren't you using it?
var box1 = $('#field_1');
var box2 = $('#field_2');
box2.val(box1.val());
}
$('#field_1').on('change', onchange);
});



[#67929] Wednesday, February 4, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ingridcassieb

Total Points: 346
Total Questions: 97
Total Answers: 125

Location: North Korea
Member since Fri, Nov 4, 2022
2 Years ago
;