Solved

dynamic select file from data stored in other select file using JavaScript

Posted on 2011-09-09
2
276 Views
Last Modified: 2012-05-12
Hi Experts,

I’m trying to generate a dynamic select file from data stored in other select file using JavaScript

For example:

<select name="sample1" id="sample1">
  <option value="A1">AAA</option>
  <option value="A2">BBB</option>
  <option value="A3">CCC</option>
  <option value="B1">DDD</option>
  <option value="B2">EEE</option>
  <option value="C1">FFF</option>
  <option value="D1">GGG</option>

</select>

I’ll pass a parameter to the function, which is the first character of “option value”

Eg. “B”

I want to create a new array like that

<select name="sample1" id="sample1">
  <option value="1">DDD</option>
  <option value="2">EEE</option>
 </select>

Any idea?

Thanks
0
Comment
Question by:alcani
2 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 36513331
check this test page : http://jsfiddle.net/cje2m/1/
    var options = [];
    options["A1"] = [
        { "value":"1", "text":"DDD" },
        { "value":"2", "text":"EEE" },
        ];
    options["A2"] = [
        { "value":"1", "text":"FFF" },
        { "value":"2", "text":"SSS" },
        ];
    options["A3"] = []; // empty
    options["B1"] = []; // empty
    options["B2"] = []; // empty
    options["C1"] = [
        { "value":"1", "text":"SDF" },
        { "value":"2", "text":"FGH" },
        ];
    options["D1"] = []; // empty

    function update_sample2(val)
    {
        sample2 = document.getElementById("sample2");
        sample2.options.length = 0;
        for(var i=0;i<options[val].length;i++)
        {
            sample2.options[sample2.options.length] = new Option(options[val][i].text, options[val][i].value);
        }

        // check if there's options in the select else hide it, you may not need this, just remove it
        sample2.style.display = (sample2.options.length == 0)?"none":"block";
    } 

Open in new window


with :
<select name="sample1" id="sample1" onchange="update_sample2(this.value)">
  <option value="A1">AAA</option>
  <option value="A2">BBB</option>
  <option value="A3">CCC</option>
  <option value="B1">DDD</option>
  <option value="B2">EEE</option>
  <option value="C1">FFF</option>
  <option value="D1">GGG</option>
</select>

<select name="sample1" id="sample2"></select>
    

Open in new window

0
 
LVL 7

Accepted Solution

by:
Gewgala earned 500 total points
ID: 36513395
function createSubMenuOptions(letter)
{
       var selectMain = document.getElementById("sample1");
       var selectSub = document.getElementById("subSample1");   

       // clear selectSub options
       selectSub.options.length = 0;

       for (var x = 0; x < selectMain.options.length; x++)
       {
             if (selectMain.option[x].value.substr(0, 1) == letter)
             {
                   var opt = document.createElement("option");
                   opt.value = selectMain.options[x].value;
                   opt.text = selectMain.options[x].text;
                   selectSub.options.add(opt);
             }
       }
}

Open in new window


The above javascript relies on your second select box (the one with filtered items) be named "subSample1" with no options defined, as such:

<select name="subSample1" id="subSample1">
 </select>

Open in new window


Now, when you pass in a letter such as "B" to the function createSubMenuOptions("B") it will add only the values of the sample1 select box that have a value that starts with "B" to the subSample1 select menu.

Hope this helps.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying 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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses how to create an extensible mechanism for linked drop downs.
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

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