0

I can't seem to get autocomplete working with forms that I'm adding to the screen with javascript. For example, I am using my new route to render my object's form. Inside my new.js.erb in rails file I have the following:

$("div#container").hide().html("<%= escape_javascript render 'form' %>").fadeIn();

Once rendered, if I type into my input element:

<input id="object_name" name="object[name]" placeholder="Name" size="30" type="text">

And listen with it with javascript:

jQuery ->
  $('input#object_name').autocomplete
    source: ['Test', 'Testing', 'Test 234']

I get nothing as far as autocomplete is concerned. If I copy and paste this form though and have it on the screen at load (and not added dynamically with my rails new path) it will work fine. How do I get autocomplete to listen to things that are added dynamically?

1 Answer 1

1

It would appear your autocomplete script is being added before your #object_name form element - if you ensure that you re-define your autocomplete script after #object_name is added to your document all should be well.

Sign up to request clarification or add additional context in comments.

Comments

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.