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

Ask A Question

Stats

  • Questions 438k
  • Answers 438k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There are several things you can do. You can have… May 15, 2026 at 4:37 pm
  • Editorial Team
    Editorial Team added an answer What you want is an object and a for ...… May 15, 2026 at 4:37 pm
  • Editorial Team
    Editorial Team added an answer The rendered="false" will cause the input element not being rendered… May 15, 2026 at 4:37 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.