0

jsFiddle

I have got a simple select menu containing all countries with their city's listed in respective option value. The value also contains some other data, for instance Canada has value +1|}|22|X while United States has this +1|}|128|X||{|58$Abilene#|{|59$Akron#|{|1$Alabama#|{|2$Alaska#|{|60$Albany#|....

I need to fill another select menu on change of country select menu. I can't figure out how can i parse each city listed in the country's value.

3
  • jQuery doesn't have any string manipulation methods, you'll have to use javascript methods to do it. Commented Apr 3, 2013 at 21:48
  • 1
    start by looking where you might get same data in simpler format Commented Apr 3, 2013 at 21:50
  • @charlietfl .. ;/ .. looking for some country-city database. Commented Apr 3, 2013 at 21:55

2 Answers 2

2

Maybe try this, idk, that data was kind of weird:

$('#country_residence').on('change', function () {
    var cities = $(this).val().split('$');
    $("#cities option").remove();
    for(var i = 1;i<cities.length;i++)
    {
     var city = String(cities[i]);
        if(city.indexOf("#")>-1)
            city = city.substring(0,city.indexOf("#"));


        $("#cities").append("<option>" + city + "</option>");
    }
});

jsFiddle: http://jsfiddle.net/EyVuS/3/

Sign up to request clarification or add additional context in comments.

Comments

0

If there was absolutely no other way to get cleaner data? I'd do some (albeit somewhat brain-melting) regular expression matching on the server side to filter the information and then parse it into a usable list for use with JSON.

I, for one, would not attempt to do this on the client side.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.