0

I have four forms (MainForm, Landed, Apartment, Condominium) in one HTML page.

<body>

        <form name="MainForm" method="post">
            <input type="radio" name="type" value="Landed">Landed
            <input type="radio" name="type" value="Apartment">Apartment
            <input type="radio" name="type" value="Condominium">Condominium
        </form>

        <form name="Landed" method="post"> </form>

        <form name="Apartment" method="post"></form>

        <form name="Condominium" method="post"></form>
</body>

MainForm has three radio buttons. If Landed radio button is pressed, the Landed form will be loaded together with MainForm. Or other radio buttons are selected, their respective forms are loaded. Before any selection is made, all forms are hidden. Then display only once a radio button is pressed. What could be the best approach for this? I tried and all forms are loaded altogether.

Thanks

3
  • What do you mean by 'loaded'? Do you mean that you want to get the HTML for them via AJAX, or just that they will be shown/hidden based on the selected option in #MainForm? Also note that your HTML is invalid - you cannot have elements outside of the <body> Commented Dec 26, 2015 at 15:16
  • @RoryMcCrossan I just want forms are shown/hidden based on the selected option of radio buttons in #MainForm. Yes I know, I just want to show for example. Commented Dec 26, 2015 at 15:17
  • @batuman: If you use a lot of html for a web-application you max be interested in AngularJS. Commented Dec 26, 2015 at 15:22

1 Answer 1

2

If all you are doing is showing/hiding the form based on the chosen radio button, you can use show and hide based on the id attributes of the form elements, like this:

<form id="mainform" name="mainform" method="post">
    <input type="radio" name="type" value="landed">Landed
    <input type="radio" name="type" value="apartment">Apartment
    <input type="radio" name="type" value="condominium">Condominium
</form>

<form id="landed" name="landed" method="post">
    Landed form
</form>

<form id="apartment" name="apartment" method="post">
    Apartment form
</form>

<form id="condominium" name="condominium" method="post">
    Condominium form
</form>
$('#mainform input[type="radio"]').change(function() {
    $('form:not(#mainform)').hide();
    $('#' + this.value).show();
});

Example fiddle

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.