Solved

ADD OPTIONS DYNAMICALLY TO SELECTBOX

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
PHP: Filling Out/Creating a PDF 29 114
dynamic created check uncheck boxes 6 43
Table style position to the left 8 26
Prevent certain words from being typed in a form 6 31
Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
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…

696 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