Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8783733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:54:01+00:00 2026-06-13T20:54:01+00:00

I am using Ruby on Rails 3.2.2 and the jquery-rails 2.1.3 (including jQuery UI).

  • 0

I am using Ruby on Rails 3.2.2 and the jquery-rails 2.1.3 (including jQuery UI). I am trying to implement a HTML form so to search, select and submit city data by using the Autocomplete jQuery UI widget.

At this time I am implementing my form with the following “autocompletable” field:

<div class="ui-widget">
  <label for="cities">City: </label>
  <input id="cities" />
</div>

<script>
  $jQ(function() {
    var cache = {};
    $jQ( "#cities" ).autocomplete({
      autoFocus: true,
      minLength: 3,
      source:    function( request, response ) {
        $jQ.ajax({
            url:      '<SEARCH_PATH>',
            data:     { search: request.term },
            dataType: 'json',
            success:  function(data) {
              var cities = new Array();

              $jQ.each(data, function(arrayID, city) {
                cities.push({ label: city.name, value: city.id })
              });

              response( cities ); 
            },
            error:    function () {
              response([]);
            }
        });
      }
    });
  });
</script>

The above code works as expected for retrieving JSON data and displaying the list of retrieved cities. As you can see, the “useful part” of city data is the id of the city since I am using the id value in order to handle internally the submitted data. However, given the “selectable” list of cities is displayed, when I use keyboard UP and DOWN arrows in order to select one of those cities then the input field is populated with the city id (not with the city name).

I would like to:

  • display the city name instead of the id in the input field on selecting the city;
  • find a way in order to submit the city id and not the city name (even if the city name is the only thing displayed in the front end content).

How could / should I make to properly accomplish that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T20:54:02+00:00Added an answer on June 13, 2026 at 8:54 pm

    Use the select event to populate a hidden field, then submit the hidden field instead.

    <div class="ui-widget">
      <label for="cities">City: </label>
      <input id="cities" />
      <input name="cities" type="hidden" id="cities-hidden" />
    </div>
    

    js

    $jQ(function() {
      var cache = {};
      $jQ( "#cities" ).autocomplete({
        autoFocus: true,
        ...
        select: function(e, ui) {
          $("#cities-hidden").val(ui.item.value); // populate hidden input
          $("#cities").val(ui.item.label); // populate autocomplete input
          return false; // prevent default action of populating autocomplete input with value
        }
      });
    });
    

    Demo: http://jsfiddle.net/f8yPX/3/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Ruby on Rails with jQuery and trying to do the following: <%=
I am trying to make a auto complete box using ruby on rails, jquery
I am using Ruby on Rails 3.0.9 and jQuery 1.6.2 I am trying to
I am trying to create a photo viewer in Ruby on Rails using jquery-lightbox
I am using Ruby on Rails 3.0.10 and jQuery 1.6. I am trying to
I am using Ruby on Rails v3.0.9 and jQuery 1.6. I am trying to
I am using Ruby on Rails 3.0.10 and jQuery 1.6. I am trying to
I am using Ruby on Rails v3.2.2 and the jquery-rails v2.0.2 gems (including jQuery
I am using Ruby on Rails 3.2.2 and the jQuery-Rails 2.0.2 gem (including the
I am using Ruby on Rails 3.2.2 and jquery-rails-2.0.2. In a my form I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.