0

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 />
</form>

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;
      }
    }
</script>

is there something wrong with this script?

2
Contributors
5
Replies
16
Views
3 Years
Discussion Span
Last Post by saqib_604
0

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

0

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?

0

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.

HTML:

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

Javascript:

<script type="text/javascript">

    function FillSpouse()
    {
        alert(event.target.getAttribute("id"));

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

</script>

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

1

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.

        $(document).ready(function()
        {
            $("#spouse-yes").click(function()
            {
                var houseno = $("#houseno").val();
                var street = $("#street").val();
                var postcode = $("#postcode").val();
                var city = $("#city").val();
                var state = $("#state").val();
                var country = $("#country").val();
                $("#spouse-houseno").val(houseno);
                $("#spouse-street").val(street);
                $("#spouse-postcode").val(postcode);
                $("#spouse-city").val(city);
                $("#spouse-state").val(state);
                $("#spouse-country").val(country);
            });
        });
        </script>
0

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

$("#spouse-houseno").val($("#houseno").val());

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.