Autocomplete field - fill the selected id into another form element (eg. hidden field) this was already asked here 
Handling id from autocomplete field

but still without answer: I have an autocomplete field which uses geonames query to search cities. After value is selected I want the city name and admin area to fill into the autocomplete field, and to fill geoname_id of selected value into another (hidden) field. I don't want the geoname_id to be visible - it looks kind of ugly...

I've tried to hack the autocomplete.js and added following line

$("#search-geoname-id").val($(this.selected).data('autocompleteValue'));

into Drupal.jsAC.prototype.hidePopup function

then also tried 

$("input[id=search-geoname-id]").val($(this.selected).data('autocompleteValue'));

but both without effect :/

Thanks for your help, Tomas
I've found solution, so I'll share it with others:

Both options in the question were correct. I just expected that the new value of hidden field will be visible through "see source code" which it's not. When I check the $form_state['values']['geonameid'] it is filled correctly.

The complete solution:

1) In Drupal.jsAC.prototype.found I've filled the 'autocompleteValue' with value instead of key, and added new data 'autocompleteKey' which I've filled with key, surprisingly :)

2) In Drupal.jsAC.prototype.hidePopup just added $('input[id="search-geoname-id"]').val($(this.selected).data('autocompleteKey'));

Hope this helps to someone in future.

P.S. I know modifying core functions is not the cleanest way to do things, but it works.