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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:51:45+00:00 2026-06-08T13:51:45+00:00

I have two select boxes. The first is a list of the optgroups of

  • 0

I have two select boxes. The first is a list of the optgroups of the second. Both are needed for my query, but I would like to filter the second select to ONLY show the region optgroup and it’s campuses when it’s selected from the first.

Here is my code:

<html>
<body>
<select name="region">
    <option value="%">All</option>
    <option value="A">Northwest</option>
    <option value="B">North Central</option>
</select>
<select name="campus">
    <option value="%">All</option>
    <optgroup label="Northwest">
        <option value="1">Gary</option>
        <option value="2">Valparaiso</option>
        <option value="3">East Chicago</option>
    </optgroup>
    <optgroup label="North Central">
        <option value="4">South Bend</option>
        <option value="5">Elkhart</option>
        <option value="6">Warsaw</option>
    </optgroup>
</select>
</body>
</html>

So if someone selects Northwest from the first one, I want to use jQuery to filter down the second one so it now looks like so:

<select name="campus">
    <optgroup label="Northwest">
        <option value="1">Gary</option>
        <option value="2">Valparaiso</option>
        <option value="3">East Chicago</option>
    </optgroup>
</select>

Not even sure if this is possible and this is my first attempt at jQuery so I am lost. Thanks in advance.

  • 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-08T13:51:47+00:00Added an answer on June 8, 2026 at 1:51 pm

    Try to .hide() the other <optgroup>s, and .show() the one(s) you want.

    Something like this:

    $('select[name="region"]').change(function() {
        var $sel = $('select[name="campus"]'),
            val = $(this).val(),
            campus = $('option:selected', this).text();
        if (val === '%') {
            $('option,optgroup', $sel).show();
        }
        else {
            $('optgroup, optgroup > option', $sel).hide();
            $('optgroup[label="' + campus + '"]', $sel).children().andSelf().show();
        }
    });​
    

    You can’t just hide the <optgroup>, you need to hide its children <option>s too.

    DEMO: http://jsfiddle.net/rffwW/

    EDIT: Seems that doesn’t work in IE (and apparently Safari). Another answer suggested wrapping the <option>s in <span>s, let’s try that:

    $('select[name="region"]').change(function() {
        var $sel = $('select[name="campus"]'),
            val = $(this).val(),
            campus = $('option:selected', this).text();
        $('span > optgroup', $sel).unwrap();
        if (val !== '%') {
            $('optgroup:not([label="' + campus + '"])', $sel).wrap('<span/>');
        }
    });​
    

    DEMO: http://jsfiddle.net/rffwW/1/

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

Sidebar

Related Questions

I have two HTML select boxes. The first is called #1 and the second
I have two input boxes and two select boxes and would like to display
I have two select boxes, the second of which is populated once the first
I have two select boxes. One with list of Cities and the second one
So I have two multiple select boxes like this <select id=foo multiple=multiple> <option value=1>Option
I have two select boxes, the first select box has two options: First set
I have two select boxes, the second of which is dependent upon the selection
I have two select boxes with dates. See here the jsfiddle: http://jsfiddle.net/egrba/ The people
I have two identical select boxes (aside from their names, of course), and I
I currently have a ajax script that dynamically builds two select boxes enabled with

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.