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

  • Home
  • SEARCH
  • 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 8702373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:38:13+00:00 2026-06-13T02:38:13+00:00

I have a form element which is being pulled in on request with ajax.

  • 0

I have a form element which is being pulled in on request with ajax. I am then trying to perform an ajax request on the inserted text box to find a location as it is typed. The code works on the first textbox but simply fails when the second one is inserted. I’ve tried to get the script to reload itself when the ajax has completed but it still won’t work. Help would be much appreciated.

Form.html.erb – Sets up the rails nested form and pulls in partial

  <%= nested_form_for(@post, :html=> {:multipart => true, :class=> "new_blog_post", :id=> "new_blog_post"}) do |f| %>

  ...

  <fieldset>
   <%= render :partial => 'search_locations', :locals => { :f => f }  %>

  </fieldset>

  <p><%= f.link_to_add "Add a location", :locations %></p>

  ...

  <% end %>

partial.html.erb – Pulled in on page load and then when ‘Add a location’ button is pressed

  <fieldset>
  <%= f.fields_for :locations do |m| %>    
        <%= m.text_field :name ,:class=>"localename", :placeholder=> "Name of the location", :autocomplete => "off" %>
        <%= m.text_field :longitude, :class => "long" %>
        <%= m.text_field :latitude, :class => "lat" %>
        <div class="latlong">
        <p class="help-block">Enter the name of the town or city visited in this blog entry.</p>
        </div>
        <%= m.link_to_remove "Remove this location" %>

   <% end %>
  </fieldset>

Javascript (placed at bottom of form)

<script type="text/javascript">


    function locationchecker() {

    // Rails to multiply the script 20 times            
    <% (0..20).each do |i| %>

    // when the #search field changes
    $(".localename:eq(<%=i%>)").keyup(function() {

    // get the value of searchfield 
        var location<%=i%> = $(".localename:eq(<%=i%>)").val(); 

    //Take the value of the textbox and pull it from geocoder
    $.get('/locations/location?location='+location<%=i%>,this.value, function(searchone<%=i%>) {
    $(".latlong:eq(<%=i%>)").html(searchone<%=i%>);

     })
        // Upon complete run the script again
    .complete(function(searchone<%=i%>) { locationchecker });


    });

    <%end%>
}
       // load script on doc ready
$(document).ready(locationchecker);


</script>

Help would be great!

Thanks in advance,

James

  • 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-13T02:38:15+00:00Added an answer on June 13, 2026 at 2:38 am

    you should use the .on()(jQuery api doc) method to attach your keyup event handler, like so :

    $('#myForm').on('keyup','.localename',function() {
           // stuff here
           var location = $(this).val(); // this = element on which the event was triggered
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form with a textfield element which when a user starts typing
I have a form element that contains about 5 fields which final query is
I am trying to place the error message elements outside the FORM element which
I have Ajax login form, which is checking whether given credentials are correct and
I have a function which updates a database via ajax. My issue is then
I have a form to which I am appending form elements on the click
I have web form with a panel which has form elements in side it.
I have a form which has elements in a couple of divs. Based on
I have a base and derived class called Node which form elements of a
I have a button which creates form elements on click . My addelement function

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.