Marc
Marc

Reputation: 1043

rails3-jquery-autocomplete redirect on select

I'm using rails3-jquery-autocomplete gem. It works perfectly, but I don't know how to implement a redirect on selection of a result. All it does now is populate the field with the text of the selected result. Thanks!

Upvotes: 0

Views: 588

Answers (3)

Andrew Hill
Andrew Hill

Reputation: 126

The best way to perform re-direct when a user selects one of the drop down items from the autocomplete list when using rails3-jquery-autocomplete is via the "railsAutocomplete.select" event, which is triggered when the user selects one of the options.

How to do this is documented here. This approach means you can avoid editing the rails3-jquery-autocomplete code, and only causes a re-direct to happen when the user selects from the drop down option, rather than "onChange" of the form field, which isn't really what you want.

Upvotes: 3

Amir Kolta
Amir Kolta

Reputation: 241

You can go to the jquery-ui.js file in assets, you will find it minified

go to http://jsbeautifier.org and make the code look better

look for the line containing this

case e.ENTER:

you will find it blank, this is the action that will be performed on clicking Enter so change it to

case e.ENTER:
 window.location.href = 'yourURL';

Upvotes: 0

klump
klump

Reputation: 3269

Just use the onChange event for the textfield which gets updated. The redirect you can achieve with some simple javascript:

window.location = "http://stackoverflow.com"

If you need different redirects for different values, use switch:

var location;

switch ( document.getElementById('#my_textfield').value ) {
  case "stackoverflow":
    location = "http://stackoverflow.com";
  case "google":
    location = "http://google.com";
  default:
    location = "http://mysite.com";
}

window.location.href = location;

Upvotes: 1

Related Questions