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 8645241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:29:59+00:00 2026-06-12T12:29:59+00:00

This is my select box code now <select id=header1_cbocity> <option value=2>Ahmedabad</option> <option value=4>Bangalore</option> <option

  • 0

This is my select box code now

     <select id="header1_cbocity">  
    <option  value="2">Ahmedabad</option>
    <option  value="4">Bangalore</option>
    <option  value="14">Chennai</option>
    <option  value="20">Delhi</option>
    <option  value="33">Gurgaon</option>
    <option  value="167">Switzerland</option>
    <option  value="261">Tanzania</option>
    <option  value="168">Thailand</option>
    <option  value="263">Uganda</option>
    <option  value="169">United Kingdom (U.K)</option>
    <option  value="170">United States (U.S)</option>
    </select>

and i want to add optgroup label only for country like below

    <select id="header1_cbocity">  
        <option  value="2">Ahmedabad</option>
        <option  value="4">Bangalore</option>
        <option  value="14">Chennai</option>
        <option  value="20">Delhi</option>
        <option  value="33">Gurgaon</option>
    <optgroup label="Country">
        <option  value="167">Switzerland</option>
        <option  value="261">Tanzania</option>
        <option  value="168">Thailand</option>
        <option  value="263">Uganda</option>
        <option  value="169">United Kingdom (U.K)</option>
        <option  value="170">United States (U.S)</option>
    </optgroup>
    </select>

i am trying with jquery code but couldn’t add optgroup label for Country, so i need help

  • 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-06-12T12:30:00+00:00Added an answer on June 12, 2026 at 12:30 pm

    I’d suggest:

    $('#header1_cbocity option:gt(4)').wrapAll('<optgroup label="country" />')
    

    JS Fiddle demo.

    I’d also suggest adding a definitive means to identify which option elements represent a country, in the demo below I’ve used a class, but a custom data-* attribute could just as easily be used. Given the markup:

    <select id="header1_cbocity">
        <option value="2">Ahmedabad</option>
        <option value="4">Bangalore</option>
        <option value="0"  class="country">Bahamas</option>
        <option value="14">Chennai</option>
        <option value="20">Delhi</option>
        <option value="33">Gurgaon</option>
        <option value="167" class="country">Switzerland</option>
        <option value="261" class="country">Tanzania</option>
        <option value="168" class="country">Thailand</option>
        <option value="263" class="country">Uganda</option>
        <option value="169" class="country">United Kingdom (U.K)</option>
        <option value="170" class="country">United States (U.S)</option>
    </select>​
    

    (Note that I’ve added Bahamas (in order to show how to handle dealing with non-consecutive states/countries).

    With the following jQuery:

    $('#header1_cbocity option.country')
        .wrapAll('<optgroup label="country" />')
        .closest('optgroup') // because otherwise wrapAll() returns the originally-found option elements
        .appendTo('#header1_cbocity');​
    

    JS Fiddle demo.

    Further, assuming that you’ve placed a definition of some kind (in the following I use a custom, data-defn, attribute) you can create optgroup elements to encompass those:

    <select id="header1_cbocity">
        <option value="2" data-defn="state">Ahmedabad</option>
        <option value="4" data-defn="state">Bangalore</option>
        <option value="0"  data-defn="country">Bahamas</option>
        <option value="14" data-defn="state">Chennai</option>
        <option value="20" data-defn="state">Delhi</option>
        <option value="33" data-defn="state">Gurgaon</option>
        <option value="167" data-defn="country">Switzerland</option>
        <option value="261" data-defn="country">Tanzania</option>
        <option value="168" data-defn="country">Thailand</option>
        <option value="263" data-defn="country">Uganda</option>
        <option value="169" data-defn="country">United Kingdom (U.K)</option>
        <option value="170" data-defn="country">United States (U.S)</option>
    </select>​
    

    With the jQuery:

    $('#header1_cbocity option').each(
        function(){
            var that = $(this),
                defn = that.attr('data-defn'),
                sel = that.closest('select'),
                optgroup = sel.find('optgroup.' + defn);
            if (!optgroup.length) {
                $('<optgroup />', {'class' : defn, 'label' : defn}).appendTo(sel);
            }
            that.appendTo(sel.find('optgroup.' + defn));
        });​
    

    JS Fiddle demo.

    Of course, with a selector that specifies only those elements with a data-defn attribute, you don’t necessarily need to supply every option with such an attribute:

    $('#header1_cbocity option[data-defn]').each(
        function(){
            var that = $(this),
                defn = that.attr('data-defn'),
                sel = that.closest('select'),
                optgroup = sel.find('optgroup.' + defn);
            if (!optgroup.length) {
                $('<optgroup />', {'class' : defn, 'label' : defn}).appendTo(sel);
            }
            that.appendTo(sel.find('optgroup.' + defn));
        });​
    

    JS Fiddle demo.

    References:

    • appendTo().
    • closest().
    • wrapAll().
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

like this is my form's select box <select name=cutid onchange=findcustid(this.value)> <option value=1001>cust 1</option> <option
I have this piece of code for select option: <select class=select-box name=select-choice-month id=select-locations> <option
I have this code for a select box which is being displayed by the
Ok, I have a select box <select id=txtPriority style=color:#008000;> <option value=0 style=color:#0000FF;>Low</option> <option value=1
I have a select box like this: <select id=se> <option>An option</option> <option>Another option</option> </select>
I have an select box <select name=type1> <option value=1>Laser Printer</option> <option value=2>Line Printer</option> </select>
This is basically the same question as Click items in select box, and then
I am creating a select box for a form using this in _form.html.erb <%=
I have a select box on a classic ASP page which looks like this:
I am trying to display(block,none) select boxes(in this example only one select box) through

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.