Solved

ADD OPTIONS DYNAMICALLY TO SELECTBOX

Posted on 2004-10-19
3
1,439 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

747 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

13 Experts available now in Live!

Get 1:1 Help Now