?
Solved

ADD OPTIONS DYNAMICALLY TO SELECTBOX

Posted on 2004-10-19
3
Medium Priority
?
1,457 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 14

Accepted Solution

by:
RichieHindle earned 2000 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…
Suggested Courses

752 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