1

I have a form with a text field that uses jQuery-UI autocomplete widget. I'm loading this form as a partial using AJAX with Ruby on Rails <li id="activity_<%= activity.id %>"><%= link_to activity.address, edit_activity_path(activity), :remote => true %></li>

Since the form is being loaded after document.ready, the autocomplete jQuery widget is not binding to the DOM element.

Since this isn't a jQuery AJAX call, I don't think that there is a success callback that I can use to bind the autocomplete widget with after the Ajax call.

I'm not sure how to use the .on() function that I've seen around with the autocomplete Widget since the syntax is $('selector').autocomplete

Can someone please help?

This is the edit.js.erb file:

$("#activity_form").html("<%= escape_javascript(render(:partial => "form"))%>");

This is my jQuery code to bind the autocomplete widget to my text field.

jQuery ->       
$('[type="text"][name*="[geocode_location]"]').autocomplete
  source: (request, response) ->
    $.ajax 
      url: "http://ws.geonames.org/searchJSON"
      dataType: "jsonp"
      data: 
        featureClass: "P"
        style: "full"
        maxRows: 12
        name_startsWith: request.term

      success: (data) ->
        response $.map(data.geonames, (item) ->
          label: item.name + ((if item.adminName1 then ", " + item.adminName1 else "")) + ", " + item.countryName
          value: item.lat + ", " + item.lng
        )
  minLength: 2

  open: ->
    $(this).removeClass("ui-corner-all").addClass "ui-corner-top"

  close: ->
    $(this).removeClass("ui-corner-top").addClass "ui-corner-all"
  focus: (event, ui) ->
    event.preventDefault()
    $(this).val ui.item.label      
  select: (event, ui) ->
    event.preventDefault()
    $(this).val ui.item.label
    $(this).siblings('[name*="[geocode_ll]"]').val ui.item.value

3 Answers 3

2

I read here about how there are 6 custom events that each Rails Ajax call can bind to, including ajax:success which is fired anytime there is a successful ajax request.

http://net.tutsplus.com/tutorials/javascript-ajax/using-unobtrusive-javascript-and-ajax-with-rails-3/

So I did $(this).bind "ajax:success" which allows $(this) which is the entire window in this case, to fire off some other functions after any "ajax:success" event.

So, I ended up doing this:

jQuery ->
  $(this).bind "ajax:success", -> 
    jQuery ->       
      $('[type="text"][name*="[geocode_location]"]').autocomplete
       --REST OF CODE--
Sign up to request clarification or add additional context in comments.

1 Comment

Brother, you have saved me a lot of pain!
1

I had a similar case, when rendering a partial containing an autocomplete input field after an ajax call the autocompletion did not work any more. I was using the rails3-jquery-autocomplete gem.

The problem was simply, that all autocomplete input fields are only bound to the autocomplete function on the initial document load. To bind new autocomplete fields again simply call the following function:

jQuery('input[data-autocomplete]').railsAutocomplete();

Comments

0

$('[type="text"][name*="[geocode_location]"]').live("autocomplete", function(){ your code})

Please try.

1 Comment

I tried this, it doesn't seem to be working. It breaks the autocomplete capability.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.