Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Populate a field with values from two other fields using jQuery

Tags:

jquery

forms

I'd like to populate a 'Full Name' field automatically with what the user types into the 'First Name' and 'Last Name' fields, so if they type 'John' and 'Smith' into the first and last name fields, the 'Full Name' field would be dynamically populated with 'John Smith'.

I'm not very good with jQuery so have only managed to populate the field with the 'First Name' so far after searching through other examples.

My current code is below, and any help on this would be appreciated.

<script>
$(document).ready(function(){
    $('#first_name').keyup(function () {
        $('#full_name').val(this.value);
    });
});
</script>

<input type="text" name="full_name" id="full_name" value="" />
<input type="text" name="first_name" id="first_name" value="" />
<input type="text" name="last_name" id="last_name" value="" />

Thanks,

Ste

like image 513
Stephen Avatar asked Oct 25 '25 05:10

Stephen


1 Answers

This should work

$('#first_name').keyup(function(){
   $('#full_name').val(this.value+' '+$('#last_name').val());
});

$('#last_name').keyup(function(){
   $('#full_name').val($('#first_name').val()+' '+this.value);
});
like image 57
Gihan Avatar answered Oct 26 '25 19:10

Gihan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!