[This took me a while to figure out, so I'm offering it as a tip for future reference. If someone has any suggestions to alter this node title to make it more findable in search, please suggest away and I'll edit it.]
Scenario:
Let's take the one I'm working on as our example because it's easier to explain. http://dev.vocalvoter.com
I have a search form which looks up street addresses for anywhere in the country. In order to help get a perfect DB match, the 'street' field autocompletes.
However, as we add more and more counties to the database, especially in a country like Ireland, the autocomplete results get progressively less useful. For example, you may find a 'St Patrick's Street' in almost every medium-sized town in Ireland, but you probably won't find more than ten in any given county.
So I altered the autocomplete handler. It takes a first argument - the numeric ID of a county - and now returns only search queries from within that area.
Altering the form with Javascript
Next we change the output of the form_api renderer. I add a <select> box containing the names and IDs of the counties (which are held in an array called $counties), and attach an onChange attribute to the field. That onChange alters the value of the hidden field called 'edit-search-autocomplete', which holds the autocomplete URL path for this form. Critically, I need to refer to both these fields by new JS variables, because Drupal's form element IDs contain dashes and in javascript that's problematic because it's a 'minus' operator. Adding the Javascript handler looks like this: