Solved

How to show (or change) different htlm form fields  based on a form selection

Posted on 2011-09-07
5
181 Views
Last Modified: 2012-05-12
I would like to be able to change form fields based on a form field selection.  For example I have this form to start:

<form name="myform" action="index.php" method="POST">                   
        <select name="select">
                    <option value="1">Select One</option>
                    <option value="2">Select Two</option>
        </select>
        <select name="type">
                    <option value="1">Type One</option>
                    <option value="2">Type Two</option>
        </select>

What I would like is to then show the next field (If_type_ one, or if_type_two) based on my selection of field "type" above.  

        <select name="if_type_one">
                    <option value="1">Type One Something</option>
                    <option value="2">Type One Something Else</option>
        </select>

        <select name="if_type_two">
                    <option value="1">Type Two Something</option>
                    <option value="2">Type Two Something Else</option>
        </select>

Thanks......


       
0
Comment
Question by:dmalovich
  • 3
5 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 36498684
Putting the selects inside div :
<div id="type1">
    <select name="if_type_one">
        <option value="1">Type One Something</option>
        <option value="2">Type One Something Else</option>
    </select>
</div>
  
<div id="type2">
<select name="if_type_two">
    <option value="1">Type Two Something</option>
    <option value="2">Type Two Something Else</option>
</select>
</div>

Open in new window

You may use :
function hide(who) {
    document.getElementById(who).style.display = "none";
}

function show(who) {
    document.getElementById(who).style.display = "block";
}

function updateType(val) {
    switch(val) {
            case "1" :show("type1");hide("type2");break;
            case "2" :show("type2");hide("type1");break;
    }
}

Open in new window


Test page : http://jsfiddle.net/427VR/
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36498698
you may use (without the div around the selects but setting their ID attributes):

<select name="if_type_one" id="type1">


<select name="if_type_two" id="type2">
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36498700
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 36498894
here is what you need I guess
<script>

function ShowOptions(n){
  document.getElementById("if_type_one").style.display=(n==1)?"":"none";
  document.getElementById("if_type_two").style.display=(n==2)?"":"none";
}
</script>
<form name="myform" action="index.php" method="POST">                    
        <select name="select">
                    <option value="1">Select One</option>
                    <option value="2">Select Two</option>
        </select>

        <select name="type" onChange="ShowOptions(this.value)">
                    <option value="1">Type One</option>
                    <option value="2">Type Two</option>
        </select>

        <select name="if_type_one" id="if_type_one" style="display:">
                    <option value="1">Type One Something</option>
                    <option value="2">Type One Something Else</option>
        </select>

        <select name="if_type_two" id="if_type_two" style="display:none">
                    <option value="1">Type Two Something</option>
                    <option value="2">Type Two Something Else</option>
        </select>

</form>

Open in new window

0
 

Author Closing Comment

by:dmalovich
ID: 36499131
It worked.  Thank you.....
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question