Solved

ADD OPTIONS DYNAMICALLY TO SELECTBOX

Posted on 2004-10-19
3
1,445 Views
Last Modified: 2008-03-10
i have 3 checkboxes
and a selectbox
if the checkboxes are checked some values need to be populated in the selectbox
but if the selectbox has that value already it should not add them.

suppose checking the checkboxA u shud add options a,b,c,d
checking checkboxB u shud add options b,c,d,e
checking checkboxC u shud add options d,e

if both checkbox A and B are checked the values in the selectbox shud not be repeated
and when u uncheck suppose checkboxB the option e should be removied not b c or d as they r in checkboxA

Thanks
Arch
0
Comment
Question by:archrajan
  • 2
3 Comments
 
LVL 14

Accepted Solution

by:
RichieHindle earned 500 total points
ID: 12351959
Here you go.  This is a self-contained web page that implements your spec:

<html>
<head>
<script>
// The values for each checkbox.
var list = [ ['a', 'b', 'c', 'd'],      // Checkbox A
             ['b', 'c', 'd', 'e'],      // Checkbox B
             ['d', 'e'] ]               // Checkbox C

// Whether each of the three checkboxes is checked.
var checked = [false, false, false]

// Function to empty a selectbox.
function emptyselectbox(selectbox)
{
   while (selectbox.options && selectbox.options.length > 0)
   {
      selectbox.remove(0)
   }
}

// Function to add to a selectbox.
function addtoselectbox(selectbox, list)
{
    var opt = document.createElement("OPTION")
    opt.text = value
    opt.value = value
    selectbox.add(opt)
}

// Handler for checkboxes being clicked.
function onClick(checkbox, index)
{
    // Get the selectbox and empty it.
    var selectbox = document.selectform.selectbox
    emptyselectbox(selectbox)
   
    // Update the array describing which checkboxes are checked.
    checked[index] = checkbox.checked
   
    // For each checkbox, if it's checked, add its values to the dictionary.
    add = {}
    for (var i=0; i < list.length; i++)
    {
        if (checked[i])
        {
            for (var j = 0; j < list[i].length; j++)
            {
                add[list[i][j]] = true
            }
        }
    }
   
    // Add the values to the selectbox.
    for (value in add)
    {
        addtoselectbox(selectbox, value)
    }
}
</script>
</head>

<body>

<form name='selectform' action='something' method='GET'>
<input type='checkbox' name='A' value='Checkbox A' onclick='onClick(this, 0); return true'>
<input type='checkbox' name='B' value='Checkbox A' onclick='onClick(this, 1); return true'>
<input type='checkbox' name='C' value='Checkbox A' onclick='onClick(this, 2); return true'>
<select name='selectbox'></select>
</form>

</body>
</html>
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 12352593
@Richie,

For adding to a select box, all you need to do is:

function addtoselectbox(selectbox, list)
{
    selectbox.options[selectbox.options.length] = new Option('new text','new value');
}
0
 
LVL 14

Expert Comment

by:RichieHindle
ID: 12355517
Zyloch: Thanks - I didn't know that.  8-)
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

813 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now