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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T23:21:45+00:00 2026-06-18T23:21:45+00:00

I have two dropdown lists that filter content. The first one is the locations

  • 0

I have two dropdown lists that filter content. The first one is the locations and the second one is the jobs. The first list filters the second. I’m using a :contains to read the string values that allow my filter to work. I’m running into a problem when I want to use two contains at once as a filter. Here is the code:

HTML

<div class="holder">
<label for="volunteerLocation">Where do you want to volunteer?</label><br>
<select id="locations">
    <option value="0">--Select a Campus--</option>
    <option value="5">Location 1</option>
    <option value="6">Location 2</option>
    <option value="7">Location 3</option>
</select>
</div>

<br />

<div class="holder">
<label for="volunteerJobs">In which area would you like to serve?</label><br />
<select id="jobs">
    <option value="1">Job 1 (Location 1)</option>
    <option value="2">Job 2 (Location 2)</option>
    <option value="3">Job 3 (Location 3)</option>
    <option value="4">Job 4 (All locations)</option>
</select>
</div>

Javascript

var select = $('#jobs');
var options = [];
$(select).find('option').each(function () {
    options.push({ value: $(this).val(), text: $(this).text() });
});
$(select).data('options', options);

$('#locations').change(function () {
    filterText = $("#locations option:selected").text();
    var optionList = $(select).empty().data('options');
    var j = 0;

    $.each(optionList, function (i) {
        var option = options[i];
        if (option.text.indexOf(filterText) !== -1) {
            if (j == 0) {
                $('#jobs').prepend("<option value=''>--Select a Job--</option>").val('');
                j++;
            };
            $(select).append(
                $('<option>').text(option.text).val(option.value)
            );
        }

        if (filterText == "--Select a Campus--") {
            $(select).append(
                $('<option>').text(option.text).val(option.value)
            );
        }
    })
})

Here is a JSLint of this so you can see it in action Full Example

I’m trying to get “Job 4” to show up on everything but the “Select a Campus” option. How do I do that?

  • 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-18T23:21:46+00:00Added an answer on June 18, 2026 at 11:21 pm

    instead of looping with .each every time location change, and going through exceptions, me would create an index upon page load

    var locJobs=new Array();
    

    then you fill it with your data, for example

    locJobs['5']=new Array();
    locJobs['5'] = ['job 1','job 2']
    

    then on change

    $("#jobs").html('<option>'+locJobs[$(this).val()].join('</option><option>')+'</option>');
    

    if you need to add the value on the options of #jobs you’ll have to complicate that snippet a bit.
    It shall be more efficient & also make maintenance much easier (no exceptions to deal with just an array to populate from whatever data source you are using) as you’ll end up with a very flexible solution

    nb: you declare var select = $("#jobs") but then you use $(select); that is a useless overhead use select directly

    a convention to keep code clear is to add $ to any variable that is caching a jquery object :

     var $select=$("#select")
    

    then you use $select.whtever(//...

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

Sidebar

Related Questions

I have two dropdown lists (one dynamically populated, the other not) that determine the
I have two dropdown lists, one containing a list of countries and one for
i have two dropdown list. first drop down:1 enter code here <form:select path=custName id=custName>
I have two custom dropdown lists that have the same markup. I need to
I have a form where there are two dropdown lists that are populated from
I have two dropdown lists, the second should only be displayed when the value
I have two drop down lists. Second one is populated based on value chosen
I have an application that has two dependent dropdown lists, where if the user
I have two dropdown lists containing customers info. Using PHP for loop, I have
I'm creating a server control that basically binds two dropdown lists, one for country

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.