Lulzim Fazlija
Lulzim Fazlija

Reputation: 885

how to auto populate text fields with text automatically

I have a simple Registration form and I want to make an option like there are two different fields, and what i want is simple, just when I add text to the first field it should automatically be added the same content to the next field.

Upvotes: 0

Views: 4091

Answers (4)

Teja
Teja

Reputation: 11

Give onchange event for first field and assign value for second field,as below

onchange="secondfieldname.value=firstfieldnamefieldname.value"

Upvotes: 0

Fabrício Matté
Fabrício Matté

Reputation: 70139

The change event only fires when 2 conditions are met:

  • The element loses focus;
  • The element has a different value property than when it got focus.

If you want the text to change as you type it, you can use jQuery together with the HTML5 input event:

//assuming `a` and `b` as text field IDs
$('#a').on('input', function() {
   $('#b').val($(this).val());
});​

JSFiddle

For non-HTML5 browsers, you can just extend the events map to simulate the input event:

$('#a').on('input keydown keypress keyup change blur', function() {
    $('#b').val($(this).val());
 });​

JSFiddle

Upvotes: 1

The Alpha
The Alpha

Reputation: 146191

$(function(){
    $('#txt1').on('change', function(e){
        $('#txt2').val($(this).val());
    });
});​

DEMO.

Upvotes: 1

Derek 朕會功夫
Derek 朕會功夫

Reputation: 94319

$("#field1").change(function(){
    $("#field2").val(this.value);
});

Demo: http://jsfiddle.net/DerekL/CcDv4/

Upvotes: 3

Related Questions