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
jQueryJavaScriptAJAX

Avatar of undefined
Last Comment
abperfecto

8/22/2022 - Mon
Jon Norman

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

Chris Stanyon

What format is the data returned by your city.view script?
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>
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
SOLUTION
Chris Stanyon

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Jon Norman

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
Chris Stanyon

No worries - often happens.

I think it's fair to assume that's the way to do it though :)
abperfecto

ASKER
Many thanks to both of you for your time and thought.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.