Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to build, hide/show Dynamic select forms

Status
Not open for further replies.

celia05es

Programmer
Jan 10, 2002
81
US
Hello,

I need to generate 3 select lists but the 2nd depends on the 1st and the 3rd depends on the 2nd.
To make it more difficult, the values are read from a database!

But just one thing at a time: First I need to generate dynamically select list.

Once I know how to do it, I will try to hide/show the other lists depending on the 1st list

Then I will try to think about how to populate those lists!

The 1st list is displayed. Once the user clicks on one name, a second list appears with the values depending on the 1st list.

Once the user chooses a value, another list should be shown The user can then submit the form.

Here is what I have written ("stolen from the web"). It works for two lists but I need it to work for 3 lists and I haven't managed to do so.

In additiong, the first time is not working since I get "No list available". I would need to have an "empty chose" as default so nothing would be shown on the 2nd list.

Once it is working, I need the 2nd and the 3rd list to be initially hidden. Once the user clicks on the value, the 2nd list is shown. Once the user clicks on the value, the 3rd list is show. Now, if the user changes the value of the 1st list, the 2nd list should be modified and the 3rd list should be hidden.

I would really appreciate if you could help me!


Code:
<html>
<script language="JavaScript" type="text/javascript">
var store = new Array();
var prix  = new Array();

store[0] = new Array(
        'Black',
        'Red');

store[1] = new Array(
        'Yellow',
        'White');

store[2] = new Array(
        'Green',
        'Blue',
        'Brown');

prix[0] = new Array(
        'Price1',
        'Price2');

prix[1] = new Array(
        'Price3',
        'Price4');

prix[2] = new Array(
        'Price4',
        'Price5',
        'Price6');

function populate()
{
   var box = document.forms[0].first;
   var number = box.options[box.selectedIndex].value;
   if (!number) return;
   var list = store[number];
   var box2 = document.forms[0].second;
   box2.options.length = 0;
   for(i=0;i<list.length;i++)
   {
      box2.options[i] = new Option(list[i]);
   }
}

</script>
<body>
<form action="#">
<select name="first" onchange="populate()">
        <option value="0">Opel</option>
        <option value="1">Renault</option>
        <option value="2">Ford</option>
</select>


<select name="second"> [b]I would like to have something like onChange="populate1(). I have tried to modify populate but does not work [/b]

        <option>No list available</option>
</select>

<select name="third">
        <option>Price1</option>
        <option>Price2</option>
</select>

</form>


</body>
</html>
 
How To Read FAQs:

1) select a forum (JavaScript - forum216)
2) click the "FAQs" link at the top
3) browse to the section about dynamic lists



*cLFlaVA
----------------------------
[tt]( <P> <B>)[sup]13[/sup] * (<P> <.</B>)[/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Ouah! That's almost what I need!!!
I have modified the script and now I have 3 dynamic lists.
Nevertheless I would like to know your opinion about the following:

- In the script that I have included, the values for the lists are hard-coded. Well, in my case, the values are read from a database.
For the 1st list, it is not really a problem since I can call a java function that would return an array with the values (if I use a jsp file instead of a html)
The problem comes with the 2nd and the 3rd list since I cannot use the swith the way it is used.
In the madeSelection function, I cannot use :
Code:
switch(selectedValue)
{
 case 'type_cat':
   loadSelectElement('select02', [
        ['breed_persian', 'Persian'],
         ['breed_tabby', 'Tabby'],
  ]);
The "type_cat" value can be passed as parameter of course, but then within javascript I would have to consult the database in order to get the related values.

What do you think is the best thing to do?

- Another thing: If the user clicks again on the 1st field:
- If it is the value "Select...", the 2nd and 3rd list should be hidden
- If it is another value, then the 3rd field should be hidden (which is not the case)
How could I do it?
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "[URL unfurl="true"]http://www.w3.org/TR/html4/strict.dtd">[/URL]
<html lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
    <meta http-equiv="content-language" content="en">
    <title>Dynamic select elements</title>

    <style type="text/css">
        html, body, form {
            padding: 0px;
            margin: 0px;
        }
        body {
            margin: 1em;
            font-family: Verdana, Arial, Helvetica, sans-serif;
        }
    </style>

    <script type="text/javascript">
    <!--

        function loadSelectElement(selObjId, options) {
            var selObj = document.getElementById(selObjId);

            // clear the target select element apart from the "select your..." option
            selObj.options.length = 1;

            // copy options from array of [value, pair] arrays to select box
            // IE doesn't work if you use the DOM-standard method, however...
            if (typeof(window.clientInformation) != 'undefined') {
                // IE doesn't take the second "before" parameter...
                for (var loop=0; loop<options.length; loop++) selObj.add(new Option(options[loop][1], options[loop][0]));
            } else {
                for (var loop=0; loop<options.length; loop++) selObj.add(new Option(options[loop][1], options[loop][0]), null);
            }
        }

function madeSelection(selObj)
{
  var selectedValue = selObj.options[selObj.selectedIndex].value;
  var selectedText = selObj.options[selObj.selectedIndex].text;
  if (selectedValue == '--') return;

  if (selObj.name == 'select01')  // First select list
  {
    document.getElementById('select02Container').style.display = 'block';
    document.getElementById('select02').options[0].text = 'Select the breed of your ' + selectedText.toLowerCase();

    switch(selectedValue)
    {
      case 'type_cat':
          loadSelectElement('select02', [
              ['breed_persian', 'Persian'],
              ['breed_tabby', 'Tabby'],
          ]);
          return;

       case 'type_dog':
          loadSelectElement('select02', [
             ['breed_alsatian', 'Alsatian'],
             ['breed_springer_spaniel', 'Springer Spaniel'],
           ]);
           return;
    }
  } // select01

  if (selObj.name == 'select02')  // "2nd select list
  {
    document.getElementById('select03Container').style.display = 'block';
    document.getElementById('select03').options[0].text = 'Select the colour of your ' + selectedText;

    switch(selectedValue)
    {
      case 'breed_persian':
      case 'breed_siamese':
         loadSelectElement('select03', [
            ['colour_white', 'White'],
            ['colour_grey', 'Grey'],
         ]);
         return;

      case 'breed_tabby':
         loadSelectElement('select03', [
            ['colour_tabby', 'Tabby']
         ]);
         return;

      case 'breed_alsatian':
      case 'breed_springer_spaniel':
         loadSelectElement('select03', [
            ['colour_brown', 'Brown'],
            ['colour_white', 'White'],
         ]);
         return;

     }
  } // select02
}

//-->
</script>
</head>

<body>
<form name="myForm">
  <select name="select01" id="select01" onchange="madeSelection(this);">
     <option value="--">Select your pet type</option>
     <option value="type_cat">Cat</option>
     <option value="type_dog">Dog</option>
  </select>

  <div id="select02Container" style="margin-top:1em; display:none;">
  <select name="select02" id="select02" onchange="madeSelection(this);">
      <option value="--">Select your pet breed</option>
  </select>
  </div>

  <div id="select03Container" style="margin-top:1em; display:none;">
  <select name="select03" id="select03" onchange="madeSelection(this);">
      <option value="--">Select your pet colour</option>
  </select>
  </div>
</form>
</body>
</html>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top