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

Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Jon Norman
Jon Norman
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Jon Norman
Jon Norman
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

No worries - often happens.

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

ASKER

Many thanks to both of you for your time and thought.
JavaScript
JavaScript

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo