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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:55:32+00:00 2026-05-18T06:55:32+00:00

I’m trying to build a custom dropdownlist which show/hide a second set of dropdowns

  • 0

I’m trying to build a custom dropdownlist which show/hide a second set of dropdowns based on it’s selection.

I was wondering if anyone here might be able to help with a solution to this.

var i = 0;
$(document).ready(function() {
  var bindClickToToggle = function(element) {
    element.click(function() {
      $(this).parents('.dropdown').find('dd ul').toggle();
    });
  };

  var bindClickToUpdateSelect = function(element) {
    element.click(function() {
      var text = element.html();
      var value = element.find('span.value').html();
      var dropdown = element.parents('.dropdown');
      var select = $(dropdown.attr('target'));
      dropdown.children('dt').find('a').html(text);
      dropdown.find('dd ul').hide();
      select.attr('value', value);
    });
  };

  var getItemHtml = function(element) {
    return '<dt><a href="#">' + element.text() + '<span class="value">' + element.attr('value') + '</span></a></dt>';
  };

  var getDropdownHtml = function(id, target) {
    return '<dl id="target' + id + '" class="dropdown" target="#' + target.attr('id') + '"></dl>';
  };

  var getEnclosingHtml = function() {
    return '<dd><ul></ul></dd>';
  };

  var createDropDown = function(element, appendTo) {
    var selected = element.find('option[selected]');
    var options = element.find('option');
    appendTo.append(getDropdownHtml(i, element));
    var target = appendTo.find('#target' + i);
    target.append(getItemHtml(selected));
    target.append(getEnclosingHtml());
    var appendOptionsTo = target.find('ul');
    options.each(function() {
      appendOptionsTo.append(getItemHtml($(this)));
    });
    appendOptionsTo.find('a').each(function() {
      bindClickToUpdateSelect($(this));
    });
    i++;
  };

  $('select').each(function() {
    createDropDown($(this), $('body'));
  });
  $('a').each(function() {
    bindClickToToggle($(this));
    $(this).click(function() {
      $(this).parents('ul').hide();
    });
  });

  $(document).bind('click', function(e) {
    var $clicked = $(e.target);
    if (!$clicked.parents().hasClass("dropdown")) {
      $(".dropdown dd ul").hide();
    }
  });
});
body {
  font-family: Arial, Helvetica, Sans-Serif;
  font-size: 0.75em;
  color: #000;
}

.desc {
  color: #6b6b6b;
}

.desc a {
  color: #0092dd;
}

.dropdown dd,
.dropdown dt,
.dropdown ul {
  margin: 0px;
  padding: 0px;
}

.dropdown dd {
  position: relative;
}

.dropdown a,
.dropdown a:visited {
  color: #816c5b;
  text-decoration: none;
  outline: none;
}

.dropdown a:hover {
  color: #5d4617;
}

.dropdown dt a:hover {
  color: #5d4617;
  border: 1px solid #d0c9af;
}

.dropdown dt a {
  background: #e4dfcb url(arrow.png) no-repeat scroll right center;
  display: block;
  padding-right: 20px;
  border: 1px solid #d4ca9a;
  width: 160px;
  padding: 5px;
}

.dropdown dt a span {
  cursor: pointer;
  display: block;
}

.dropdown dd ul {
  background: #e4dfcb none repeat scroll 0 0;
  border: 1px solid #d4ca9a;
  color: #C5C0B0;
  display: none;
  left: 0px;
  padding: 5px 0px;
  position: absolute;
  top: 2px;
  width: auto;
  min-width: 170px;
  list-style: none;
}

.dropdown span.value {
  display: none;
}

.dropdown dd ul li a {
  padding: 5px;
  display: block;
}

.dropdown dd ul li a:hover {
  background-color: #d0c9af;
}

.dropdown img.flag {
  border: none;
  vertical-align: middle;
  margin-left: 10px;
}

.flagvisibility {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<p class="desc">The control down here is a dropdown made with CSS and jQuery. It is bound to SELECT element on the page which isn't hidden intentionally.</p>
<div id="log"></div>
<select id="source">
  <option selected="selected" value="BR">Brasil</option>
  <option value="FR">France</option>
  <option value="DE">Germany</option>
  <option value="IN">India</option>
</select>
<select id="source2a">
  <option selected="selected" value="BR">Cities in France</option>
  <option value="FR">France City 1</option>
  <option value="DE">France City 2</option>
  <option value="IN">France City 3</option>
  <option value="JP">France City 4</option>
  <option value="RS">France City 5</option>
  <option value="UK">France City 6</option>
  <option value="US">France City 7</option>
</select>
<select id="source2b">
  <option selected="selected" value="BR">Cities in Germany</option>
  <option value="FR">Germany City 1</option>
  <option value="DE">Germany City 2</option>
  <option value="IN">Germany City 3</option>
  <option value="JP">Germany City 4</option>
  <option value="RS">Germany City 5</option>
  <option value="UK">Germany City 6</option>
  <option value="US">Germany City 7</option>
</select>
<select id="source2c">
  <option selected="selected" value="BR">Cities in India</option>
  <option value="FR">India City 1</option>
  <option value="DE">India City 2</option>
  <option value="IN">India City 3</option>
  <option value="JP">India City 4</option>
  <option value="RS">India City 5</option>
  <option value="UK">India City 6</option>
  <option value="US">India City 7</option>
</select>
  • 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-18T06:55:32+00:00Added an answer on May 18, 2026 at 6:55 am

    use the jquery :selected a little bit of documentation is here http://api.jquery.com/selected-selector/

    That works in an option select menu

    I am updating your Jfiddle now if you can give me a little more info about what you want done.


    Edit

    Here is an updated jfiddle with your answer.
    http://jsfiddle.net/stAAm/7/

    and a copy of the code for Stack overflow

    $('#source').change(function () {
            if ($('#source option:selected').text() == "France"){
                $('.cities').hide();
                $('#source2a').show();
            } else if ($('#source option:selected').text() == "Germany"){
                $('.cities').hide();
                $('#source2b').show();
            } else if ($('#source option:selected').text() == "India"){
                $('.cities').hide();
                $('#source2c').show();
            } else {
                $('.cities').hide();
            } }); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to select an H1 element which is the second-child in its group
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

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.