Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto populate field base on what was entered in another field simultaneously

I was trying to figure out how to auto-populate the input value base on what was entered in another input field using javascript. Here's my code:

<script>
add = document.getElementById('address').value;
city = document.getElementById('city').value;
state = document.getElementById('state').value;
zip = document.getElementById('zip').value;
compAdd = add+" "+city+" "+state+" "+zip;
function showAdd(){
    document.getElementById('shipadd1').value=compAdd;
    document.getElementById('add1').innerHTML=compAdd;
}
</script>

<form action="" onchange="showAdd();">

Home Address: <input id="address" name="address" type="text"  /><br/>
Apt or Suite Number: <input id="suite" name="suite" type="text" /><br/>
City/Town: <input id="city" name="city" type="text" /><br/>
State:<select id="state" name="state" value="">...</select><br/>
Zip:<input id="zip" name="zip" type="text" value=""/><br/>


<p> Select Shipping Address Below: </p>

<input type="checkbox" id="shipping-address-1" name="address1" value=""><span id="shiadd1">(Print auto-populated shipping address here...)</span>

<input type="checkbox" id="shipping-address-2" name="address2" value="">ABC Corp 123 Main Street, My City, State Zip
</form>
like image 912
netizen0911 Avatar asked Aug 27 '13 20:08

netizen0911


People also ask

How do you auto populate a field based on another field in Servicenow?

If you want to auto populate the value in another field on same form, you can use catalog client script.

What can be used to automatically populate fields in a form?

You can auto-populate form fields on a page by adding query strings to the page URL before sending it to your contacts. Fields will populate based on the query string added.

How do you auto populate multiple fields in a form?

Open your PDF form in Adobe Acrobat Pro, choose Prepare Form > Fields and name the field(s) that you need the information to be copied to EXACTLY like the field where the information will be copied from. The system will then mark it with a “#” sign which means that fields are auto-populated.


2 Answers

I made a js fiddle based on what you're asking for.

HTML

Home Address:
<input id="address" name="address" type="text" />
<br/>Apt or Suite Number:
<input id="suite" name="suite" type="text" />
<br/>City/Town:
<input id="city" name="city" type="text" />
<br/>State:
<select id="state" name="state" value="">
    <option value="AL">Alabama</option>
    <option value="AK">Alaska</option>
</select>
<br/>Zip:
<input id="zip" name="zip" type="text" value="" />
<br/>
<p>Select Shipping Address Below:</p>
<input type="checkbox" id="shipping-address-1" name="address1" value="">
<label for="shipping-address-1" id="shiadd1">(Print auto-populated shipping address here...)</label>
<input type="checkbox" id="shipping-address-2" name="address2" value="">
<label for="shipping-address-2">ABC Corp 123 Main Street, My City, State Zip</label>

JavaScript

$("#address,#suite,#city,#state,#zip").change(function () {
    var addressArray = [$("#address").val(), $("#suite").val(), $("#city").val(), $("#state").val(), $("#zip").val()];
    $("#shiadd1").text(addressArray.join(' '));
});
like image 151
JustinMichaels Avatar answered Sep 28 '22 07:09

JustinMichaels


I made a general example :

HTML

<input type="text" class="first">
<input type="text" class="second">

javascript

$(".first").on('keyup',function(){
    $(".second").val($(this).val());
});

http://jsfiddle.net/fmdwv/1/

For your purpose :

$("#address").on('change', function(){
  $("#shipping-address-1").val($(this).val());
});
like image 27
steo Avatar answered Sep 28 '22 08:09

steo