Solved

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

Posted on 2011-09-07
5
182 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:Huseyin KAHRAMAN
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

749 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