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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:49:22+00:00 2026-05-29T19:49:22+00:00

I want to create a drop-down box with grouped items. It is for a

  • 0

I want to create a drop-down box with grouped items. It is for a database lookup HTML form that I am working with. Is there a way that I can select the group title, resulting in selection of all items in that particular group? For example, if the group title is ‘German Cars’ and has the items ‘Mercedes’ and ‘Audi’, on clicking ‘German Cars’, both German car companies should get selected.

  • 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-29T19:49:23+00:00Added an answer on May 29, 2026 at 7:49 pm

    If you give your opt-groups a class you can select the class then access its children.

    $('.german-cars').children();
    

    If that’s unavailable the alternative is a little slow. When you loop through all the optgroups and compare their text individually then grab that ones children.

    edit: Fix selector.
    And elaborating:

    Then to select them on click you’d use:

    $('.german-cars').click(function(e) {
        e.preventDefault();
        $(this).children().attr('selected', 'selected');
    });
    

    Assuming the element is set to multiple selection. Otherwise only the last car in the list would get selected.

    EDIT 2

    Fiddle: http://jsfiddle.net/mbChZ/25/

    These changes should work the way you’re looking for.

    $(function() {
        $('select optgroup').mousedown(function(e) {
            e.preventDefault();
            if ($(this).children(':selected').length == $(this).children().length) {
                $(this).children().attr('selected', null);
            } else {
                $(this).children().attr('selected', 'selected');
            }
        });
        $('select option').mousedown(function(e) {
           e.stopPropagation();
           e.preventDefault();
           $(this).attr('selected', ($(this).is(':selected'))? null : 'selected');                    
        });
    });​
    

    I’ve changed the events to mousedown so we can see the state of the elements before the browser has already handled the mousedown event. This allows us to check if they’re all selected and if so deselect rather than select. And the same had to be done for the click event of the option so that it can prevent propagation on that event.

    I’m not 100% sure of the browser compatability on this one though you may want to make sure it works in older IE’s if you support them.

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

Sidebar

Related Questions

I want to be able to create a drop-down select box based on the
I need to create an HTML drop down that will display flags of countries.
Lets say that I have a drop down list that I want to create
I want to create a form for my user which shows a drop down
I'm trying to create a drop-down box with a few values. I don't want
I am trying to create a drop down control that is made up of
I am trying to create a form in drupal 6 that has multiple items
I want to create a drop down area like once in the top of
I have a model called managers, I want to create a drop down list
I am trying to create drop down menus for my app, and I want

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.