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 8370413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:54:21+00:00 2026-06-09T13:54:21+00:00

I am trying to make a form that adds elements to the form after

  • 0

I am trying to make a form that adds elements to the form after certain elements have been selected. For example, After a user selects a country, I want there to be an ajax call where all of the regions/states associated with that country are found, and then inserted into a select element into the form. Then, once a user selects a region/state, the same process will take place and another element will be added to the form in reponse to the selected value. As a side note, I want to do this all unobtrusively.So far, I have this in my view:

<%= form_tag(find_school_path, :method=>'get', :class => 'form-horizontal', :id => 'find_by_country_form') do %>

    <%= label_tag :country_selection, "Country" %>
    <%= select_tag :country_selection, options_from_collection_for_select(@countries, :id, :name), :prompt => "Country" %>

<% end %>

Then inside of that view’s javascript file I handle the ajax call upon selection of a country:

$('#country_selection').change(function(){
  $.ajax({
    url: "find/country_selection",
    type: "GET",
    data: {'country=' + $('#country_selection option:selected').value() },
  })
});

Then, inside of my controller in which the above url: routes to I have:

def country_selection
    @country = Country.find(params[:country])
    @regions = @country.regions

    respond_to do |format|
       format.js {  }
    end
end

Lastly, in my country_selection.js.erb file I have:

$('#country_selection').after('<%= label_tag :region_selection, "State/Region" %><%= select_tag :region_selection, options_from_collection_for_select(@regions, :id, :name), :prompt => "State/Region" %>');

I have not gotten this to work. I am not even sure I am doing this the right way. When I hit select and change the option, nothing on the page changes.

SOLUTION

My code was fundamently fine, but the ‘.change’ event was not being recognized. I change it to ‘.live(‘change’, function() {…})’ and it worked just fine.

  • 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-09T13:54:22+00:00Added an answer on June 9, 2026 at 1:54 pm

    It’s not the tidiest code I have seen, but I see nothing wrong with it fundamentally.

    I would throw some alerts in there so you can narrow down where the problem is. You could put one in the change function to make sure that is getting hit, you could add one to the country_selection.js.erb to make sure that is getting hit. This will help a bit.

    I would also change the country_selection.js.erb to render a partial inside a div container where you want the new select to be, that way you can put that code into a partial and it will be a bit tidier. You can just pass a parameter to the partial that would be the selected country.

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

Sidebar

Related Questions

I'm trying to make a form that the allows the user to add or
I am trying to make a form that lets the user choose a date
I'm trying to make a page that takes a form submission and adds the
Hi I am trying to make a basic form that adds a value of
I kinda have a problem. I'm trying to make a form that copies stuff
I am trying to make a small form that lets the user pick one
I'm trying to make a form for updating user accounts in Django that is
Basically I am trying to make a simple form that allows the user to
I'm trying to make an AJAX form that will update a twitter status when
Trying to make a custom :confirm message for a rails form that returns data

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.