hey guys.
I have a form (very much simplified):

<form method="post" action="add_contact.php">
<label>House No.  :</label><input type="text" name="houseno" required />
<label>Street Name :</label><input type="text" name="street" required />
<lable>Spouse ?</lable><input type="radio" id="spouse-yes" onclick="FillSpouse(this.form)">Yes<input type="radio" id="spouse-no">

<label>House No.  :</label><input type="text" name="spousehouseno" required />
<label>Street Name :</label><input type="text" name="spousestreet" required />

what i want is for when radion button "Yes" is clicked the two other text fields(name="spousehouseno" and name="spousestreet") will be populated by the same input values in the text fields(name="houseno" and name="street").

ive tried using from Click Here:

<script type="text/javascript">
function FillSpouse(f) {
      if(f.spouse-yes.checked == true) {
        f.houseno.value = f.spousehouseno.value;
        f.street.value = f.spousestreet.value;

is there something wrong with this script?

3 Years
Discussion Span
Last Post by saqib_604

In example there is checkbox and you are using radio button. why? you can't use radio button.


yeah i know that example uses checkbox. does that mean i can't use radio button to do what i want? do u have any suggestions?


First manage the radio buttons rightly. I think these are for selection of "yes" and "no". right?

set the values of yes and no to both radios or use ids to identify which radio button is selected in the js code.

if there is "yes" option is selected, copy the values from source to destination. else clear the destination fields.


<input type="radio" id="spouse-no" name="spouse" onclick="FillSpouse()" />No<br>


<script type="text/javascript">

    function FillSpouse()

        // use id to identify which radio is clicked.
        // get values by ids of source fields and copy 
        // to destination.


Hope thats the answer of question now. :-) . Dont forget to mark solved if solved. :-)


I did it like this instead using jquery. i dont know if there is a more simpler way to do this so i guess this is solved. unless someone has an easier way to do this feel free to add to the discussion.

                var houseno = $("#houseno").val();
                var street = $("#street").val();
                var postcode = $("#postcode").val();
                var city = $("#city").val();
                var state = $("#state").val();
                var country = $("#country").val();

You can do to assign value from one field to another like:


other thing is that, after clicking on "yes", you are copying the values from source to destination. but what if you click on "No"? Manage it also, thats a good practice. :) .

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.