Link to home
Start Free TrialLog in
Avatar of abperfecto
abperfecto

asked on

Jquery script to take select option and make API call with parameter

Hi,

I have a html select drop down with a list of countries.
Want I need is for when I select a country, it takes the value and envokes a call to my webapp 'search/city.view?value={select box value}' which then populates an empty city select drop down.
Im using jQuery JavaScript Library v1.3.1 - I know it's out dated but its used throughout the site at the moment and to change would be time consuming at the moment, there is plans to down the line.

Many thanks
Avatar of Jon Norman
Jon Norman
Flag of United Kingdom of Great Britain and Northern Ireland image

jQuery's load function was around in version 1.3. I wonder if that worked well.

If you had selects of id country and city then the following might work:

$('#country').change(function(){
  $('#city').load('search/city.view?value='+$(this).val());
}

Open in new window


if search/city.view?value=UK returned the following html:

<option value="London">London</option>
<option value="Glasgow">Glasgow</option>
...etc

Open in new window

What format is the data returned by your city.view script?
Avatar of abperfecto
abperfecto

ASKER

This is an example...

<?xml version="1.0" encoding="UTF-8"?>
<list lang="en" datalang="en" countrycode="AU">
  <item value="0">Please select</item>
  <item value="ADL">Adelaide</item>
  <item value="EPZ">Alexandra Headland</item>
  <item value="ASP">Alice Springs</item>
  <item value="EPV">Apollo Bay</item>
  <item value="AYQ">Ayers Rock</item>
  <item value="BNE">Brisbane</item>
  <item value="BME">Broome</item>
  <item value="QYN">Byron Bay</item>
  <item value="CNS">Cairns</item>
  <item value="CUD">Caloundra</item>
  <item value="CBR">Canberra</item>
  <item value="DRW">Darwin</item>
  <item value="EXW">Dunsborough</item>
  <item value="EXZ">Fraser Island</item>
  <item value="OOL">Gold Coast</item>
  <item value="EYJ">Halls Gap</item>
  <item value="HVB">Hervey Bay</item>
  <item value="HBA">Hobart</item>
  <item value="KNX">Kununurra</item>
  <item value="LST">Launceston</item>
  <item value="FFG">Lorne</item>
  <item value="FGP">Magnetic Island</item>
  <item value="MEL">Melbourne</item>
  <item value="MJK">Monkey Mia</item>
  <item value="PER">Perth</item>
  <item value="PTI">Port Douglas</item>
  <item value="RTS">Rottnest Island</item>
  <item value="FKU">Scarborough</item>
  <item value="MCY">Sunshine Coast</item>
  <item value="SYD">Sydney</item>
  <item value="TMW">Tamworth</item>
  <item value="GZC">Whitsundays</item>
</list>
SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry Chris,

Our answers seem to have crossed over, the main difference between the two is that my code doesn't assume that the city select is empty.

Jon
No worries - often happens.

I think it's fair to assume that's the way to do it though :)
Many thanks to both of you for your time and thought.