Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
166
rated 0 times [  167] [ 1]  / answers: 1 / hits: 27223  / 15 Years ago, tue, january 5, 2010, 12:00:00

Is there an easy way to populate one form field with duplicate content from another form field?



Maybe using jQuery or javascript?


More From » jquery

 Answers
258

You just have to assign the field values:



// plain JavaScript
var first = document.getElementById('firstFieldId'),
second = document.getElementById('secondFieldId');

second.value = first.value;

// Using jQuery
$('#secondFieldId').val($('#firstFieldId').val());


And if you want to update the content of the second field live, you could use the change or keyup events to update the second field:



first.onkeyup = function () { // or first.onchange
second.value = first.value;
};


With jQuery:



$('#firstFieldId').keyup(function () {
$('#secondFieldId').val(this.value);
});


Check a simple example here.


[#97920] Thursday, December 31, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cruz

Total Points: 415
Total Questions: 98
Total Answers: 109

Location: France
Member since Thu, May 6, 2021
3 Years ago
cruz questions
Tue, Sep 14, 21, 00:00, 3 Years ago
Fri, Mar 26, 21, 00:00, 3 Years ago
Sat, Oct 26, 19, 00:00, 5 Years ago
;