views:

2139

answers:

4

I'm working on a rails app.

I have a dropdown with countries. Selecting a country would fill the city dropdown with the cities in that country.

I know how to do this server-side, but I'd like to do it client-side if possible. I've used the RJS helpers, but I've no experience with jQuery (not exactly even sure what it is) or the other selector javascript libraries.

Clarification: I'd like to do this entirely client-side. That is, send all the values in the initial page request. I'm not looking to call the server ajaxically, hit the database, then return those results. I'd like them loaded up with the page.

A: 

Creating a long list of elements with javascript will most likely perform worse on the client. Why would you prefer it?

Aram Verstegen
My list isn't that long, certainly nothing as long as a list of countries and all the cities in those countries. I'd like to avoid hitting the server to look up something that could be sent with the initial page request.
wesgarrison
+1  A: 

Maybe this can help (a simple google search did it):

http://remysharp.com/2007/01/20/auto-populating-select-boxes-using-jquery-ajax/

Luca Matteis
This is for php, but should be almost identical for rails -- just the server side json generation would change.
Chris Hynes
Clarification: I'd like to do this **entirely** client-side. That is, send all the values in the initial page request. I'm not looking to call the server ajaxically, hit the database, then return those results. I'd like them loaded up with the page.[New to SO: is this the proper way to reply?]
wesgarrison
A: 

I recommend you to use jQuery for this. But you need to take look documentation of jquery and then take a look jQuery ajax api

Barbaros Alp
+1  A: 

I used a combination of jQuery and the jQuery calculations plugin to get this working.

wesgarrison