I have a form, with several input fields that are title
, name
, address
etc
What I want to do, is to get these values and 'put them' into values of other input fields. For example
<label for="first_name">First Name</label> <input type="text" name="name" /> <label for="surname">Surname</label> <input type="text" name="surname" /> <label for="firstname">Firstname</label> <input type="text" name="firstname" disabled="disabled" />
So If I enter John
in the first_name
field, then the value of firstname
will also be John
.
Many thanks
jQuery val() Method The val() method returns or sets the value attribute of the selected elements. When used to return value: This method returns the value of the value attribute of the FIRST matched element.
jQuery val() method is used to get the value of an element. This function is used to set or return the value. Return value gives the value attribute of the first element. In case of the set value, it sets the value of the attribute for all elements.
Assuming you can put ID's on the inputs:
$('#name').change(function() { $('#firstname').val($(this).val()); });
JSFiddle Example
Otherwise you'll have to select using the names:
$('input[name="name"]').change(function() { $('input[name="firstname"]').val($(this).val()); });
It's simpler if you modify your HTML a little bit:
<label for="first_name">First Name</label> <input type="text" id="name" name="name" /> <label for="surname">Surname</label> <input type="text" id="surname" name="surname" /> <label for="firstname">Firstname</label> <input type="text" id="firstname" name="firstname" disabled="disabled" />
then it's relatively simple
$(document).ready(function() { $('#name').change(function() { $('#firstname').val($('#name').val()); }); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With