Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 802721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:38:58+00:00 2026-05-14T23:38:58+00:00

I have this function for adding options to a Select list: function addOption(selectbox, value,

  • 0

I have this function for adding options to a Select list:

 function addOption(selectbox, value, text, cl )
 {
var optn = document.createElement("OPTION");
optn.text = text;
optn.value = value;
if (cl==1){ optn.className = "nav_option_main"; }
selectbox.options.add(optn);
 }

I have just discovered the “optgroup” tag, but I don’t know how to implement it to the drop list.

The function above is called on another selectboxes “OnChange” event, to fill sub-select-box with the desired options.

I don’t think it should be too difficult to add the “optgroup” tag into this, or is it?

Thanks and if you need more input let me know…

UPDATE:

When triggering your function Beejamin the optgroup label is copied beneath one another.
Ex:

 Label
 Label
 Label
   optgroup.1
   optgroup.2
   optgroup.3
   etc...

Thanks for the function though… But how can I fix this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T23:38:58+00:00Added an answer on May 14, 2026 at 11:38 pm

    Here’s an example:

    http://www.spookandpuff.com/examples/optGroup.html

    And here’s the function – which is adapted from your original one:

       function addOption(selectbox, contents, cl )
        {
    
         var addNode = selectbox; //Cache the selectbox, in case we're only adding a single, ungrouped, option.
         if (contents.length > 1) { //If we're adding a group
           addNode = document.createElement("OPTGROUP"); //Set the node to add to to a new optgroup.
           addNode.label = contents[0]; //Use the first label (a string) as the group's label.
           contents.shift(); //Remove the label from the array (so it doesn't get re-used)
         }
    
          for (var i=0;i < contents.length;i++) {
            var optn = document.createElement("OPTION");
            optn.text = contents[i][0];
            optn.value = contents[i][1];
            if (cl==1){ optn.className = "nav_option_main"; }  
            addNode.appendChild(optn);  //Append this option to either the selectbox, or the group.
          }
    
         if (contents.length > 1) { //If we've just added a group, we need to add the group to the select box.
           selectbox.appendChild(addNode);
         }
    
      }
    

    This version accepts a parameter ‘contents’, rather than a separate params for ‘text’ and ‘value’ – this accepts an array of one or more options. So, to add a single option, not as part of a group, do this:

    var singleOption = [
        ['singleOption', 1]
      ];
    
    addOption(document.getElementById('selector'), singleOption, 0);
    

    The item in the contents array is itself an array of two items – the text, and the value.

    If you stack more than one of these into the contents array, you create an optGroup:

    var optionGroup1 = [
        'Group Title',
        ['groupOne.one', 1],
        ['groupOne.two', 2],
        ['groupOne.three', 3],
        ['groupOne.four', 4],
        ['groupOne.five', 5]
      ];
    
    addOption(document.getElementById('selector'), optionGroup1, 0);
    

    The first item is the title for the group, after that, it’s just repeating the same content as the single item. When the function detects you’re trying to add more than one at a time, it groups them together.

    Is that what you’re after?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a javascript function. This function is supposed to populate a html select-box
I'm dynamically adding rows/fields in this code. I have a text field for the
I have this select that is constantly changing (adding, removing, etc)... It goes something
I have this function // add history paths and save data function AddPath( strTag,
I have this function: int firstHeapArray (IntHeapArray h) { if(!emptyHeapArray(h)) return h.array[0]; } It's
I have this function to create a request to another file to update the
I have this function: function validate($data) { $newData = str_replace(&nbsp;, , $newData); $newData =
I have this function that I would like to condense into some iterator. How
I have this function, and i animate via css a flipbox. How could i
I have this function which is supposed to set a certain time format to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.