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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:03:53+00:00 2026-06-12T21:03:53+00:00

I am using the http://geoplugin.net plugin to detect the users location. I have a

  • 0

I am using the http://geoplugin.net plugin to detect the users location. I have a <select> with a list of states, and using the jQuery

$.getJSON(url, function(data){
    var state = data['geoplugin_region'];
    $('#states').val(state).attr('selected',true);
    var selectId = "#" + state ;
    var citySelect = selectId + "-cities";
    $(citySelect).show();
});

Then it uses another plugin I wrote to display the correct select that has the cities for that state. My format for this is the select id is WA-cities for Washington.

Then I wrote this:

var detectedCity = data['geoplugin_city'];
var city = detectedCity.replace(" ","-");
var selectedCity = city.toLowerCase();
var stateDash = state + "-";
var selectedValue = stateDash + selectedCity;
$(citySelect).val(selectedValue).attr('selected',true);

but when a city that isn’t in the select is detected, it just returns the first value. How can I check if that city is in the select and then only select it if it’s there. My select is setup like this (for Washington):

<select name="cities" id="WA-cities" class="cities">
    <option value="" selected="selected" disabled>Select a City</option>
    <option value="WA-bellingham">Bellingham</option>
    <option value="WA-seattle">Seattle</option>
</select>

There are actually more cities in the select. But I’m detected in Mercer Island, which isn’t on the select. the JavaScript says $(citySelect).val('WA-mercer-island).attr('selected',true); but just chooses the Bellingham one because there’s no Mercer Island. What should I do?

  • 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-12T21:03:55+00:00Added an answer on June 12, 2026 at 9:03 pm
    $(citySelect).val(selectedValue).attr('selected',true);
    

    This doesn’t mean “Set the option which value = selectedValue”. It’s wrong.

    This is the right way,

    $(citySelect + 'option[value="'+selectedValue+'"]').attr('selected',true);
    

    here is a jsFiddle Example

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

Sidebar

Related Questions

Im using http://mbielanczuk.com/jquery-gantt/ plugin Is anyone there have an idea how to change the
I'm using http://razorjack.net/quicksand/ this jquery. I want to select 'application' instead of 'everything' in
I am using http://www.antiyes.com/jquery-blink-plugin for blinking images on my document. Code of that plugin
I'm using http://benalman.com/projects/jquery-hashchange-plugin/ to listen for hash changes in my project, but his plugin
Using http://objectmix.com/javascript/389546-reading-json-object-jquery.html as a starting point, I have been reading lots about JSON. Unfortunately
I'm currently using http://benalman.com/projects/jquery-hashchange-plugin/ and I noticed that my hashes show up as /#page
I'm using http://jqueryui.com/demos/sortable/#empty-lists to render a list of elements that an user can select.
I have been using http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application as guidance to help me create a repository.I have
I am using http://blog.frankel.ch/custom-loginmodule-in-tomcat tutorial for tomcat JAASRealm.I have added below in server.xml Realm
I'm using http://lokeshdhakar.com/projects/lightbox2/ . I have image thumbnails and on clicking them they open

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.