Autofill inputs with a checkbox

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Autofill inputs with a checkbox

Post by icesolid »

I want to be able to auto fill in inputs if the user decides that both their mailing address and location address are the same. I have the accomplished my only problem is that if the user unselects the checkbox my program does not UN auto fill the input boxes.

My program leaves the location address fields enabled until the users checks off the box, then the program disables the inputs and fills in the information from the mailing address fields. I want the program to enable the inputs and UN fill the inputs if the user UN checks the check box.

Here is my code so far:

JAVA:

Code: Select all

function DisableInput() {
    document.new_request.location_address.disabled = true;
    document.new_request.location_city.disabled = true;
    document.new_request.location_state.disabled = true;
    document.new_request.location_zip.disabled = true;

    document.new_request.location_address.value = document.new_request.mailing_address.value;
    document.new_request.location_city.value = document.new_request.mailing_city.value;
    document.new_request.location_state.value = document.new_request.mailing_state.value;
    document.new_request.location_zip.value = document.new_request.mailing_zip.value;
}
HTML:

Code: Select all

<input type="checkbox" name="make_same" class="fields" onClick="DisableInput();">
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

you need to reset the value to nothing:

Code: Select all

<script>
function autoFill(obj)
{
  if(obj.checked)
    {
       // do your copying fields here....
    }
    else
    {
       document.someForm.someField.value = '';
        //etc
    }
}
<script>

<input type="checkbox" name="somename" onClick="autoFill(this)">
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Thank You

Post by icesolid »

Thanks a bunch, it works great!

My new code:

Code: Select all

function autoFill(obj) { 
    if(obj.checked) { 
        document.new_request.location_address.disabled = true;
        document.new_request.location_city.disabled = true;
        document.new_request.location_state.disabled = true;
        document.new_request.location_zip.disabled = true;

        document.new_request.location_address.value = document.new_request.mailing_address.value;
        document.new_request.location_city.value = document.new_request.mailing_city.value;
        document.new_request.location_state.value = document.new_request.mailing_state.value;
        document.new_request.location_zip.value = document.new_request.mailing_zip.value;
    } else { 
        document.new_request.location_address.disabled = false;
        document.new_request.location_city.disabled = false;
        document.new_request.location_state.disabled = false;
        document.new_request.location_zip.disabled = false;

        document.new_request.location_address.value = "";
        document.new_request.location_city.value = "";
        document.new_request.location_state.value = "";
        document.new_request.location_zip.value = "";
    } 
}
Post Reply