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

39,825

Solution 1

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(' '));
});

Solution 2

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());
});
Share:
39,825
netizen0911
Author by

netizen0911

Updated on November 02, 2020

Comments

  • netizen0911
    netizen0911 over 3 years

    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>
    
    • j08691
      j08691 over 10 years
      Why is this tagged jQuery? I see none.
    • Chris Rockwell
      Chris Rockwell over 10 years
      Are you trying to do this without jQuery?
    • netizen0911
      netizen0911 over 10 years
      If it can be done with JQuery then, yes. Either way should work for me.
  • Ingus
    Ingus almost 7 years
    Hey , this works excellent with simple inputs!But why its not working with type="date" ?:(
  • user9437856
    user9437856 over 5 years
    It's not taking the last letter. I mean If I enter an address, USA then it's displaying on US
  • steo
    steo over 5 years
    @user9437856 updating: you should use keyup and not keydown
  • user9437856
    user9437856 over 5 years
    @steo, Thanks for the reply, actually I found my solution using this code. I have to display the firstname and lastname in the fullname fields function updateFullName() { $("#fullname").val($("#firstname").val()+ ' ' + $("#lastname").val()); } $("#firstname, #lastname").on('input', updateFullName);