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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:05:02+00:00 2026-05-24T18:05:02+00:00

I have two select elements in my form, Category and Sub-category. At first, only

  • 0

I have two select elements in my form, Category and Sub-category. At first, only the category select is shown. When the user makes a selection in the category select, an AJAX request is sent to the server and the subcategory select is shown. It works fine.

Now, when there is some error in the form submit, due to anything, missing some value that is required, or anything, I show the error message, but I cannot retain the same state of the select boxes, I see only the category select, with no value selected, i.e the initial state. Can anyone suggest me how I can preserve the state of these select elements?

Here’s a code snippet from my new form:

    <div id="category-select">
    category <%= collection_select :post_category, :id, @categories, :id, :name, 
                                                options = {:prompt => "Select a category"} %>
    </div>
    <div id="sub-category-select">
    </div>

Here’s my jQuery script that sends AJAX request when a selection is made on Category select:

$("#post_category_id").change(function() {
  var category_id = $('select#post_category_id :selected').val();
  if(category_id == "") category_id="0";
    $.get('/posts/update_sub_cat/' + category_id, function(data){
       $("#sub-category-select").html(data);
    })
  return false;
});

The AJAX request is made on the update_sub_cat action in the post_controller, which is shown below:

def update_sub_cat
  if params[:id].present?
    @sub_categories = Category.find_all_by_parent_id(params[:id])
  else
    @sub_categories = []
  end
  respond_to do |format|
    format.js
  end

end

The AJAX request renders update_sub_cat.js.erb file, in which I have used some HMTL

sub-category <%= collection_select :post_sub_category, :id, @sub_categories, :id, :name, 
                                        options = {:prompt => "Select a sub-category"} %>

I know, I should not directly use HTML here, but rather use $(‘sub-category-select).append(…), but I got it working like this, and am planning to change it later.

This is all the code that is involved in this part of my program.

Can anyone help me, please?

  • 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-05-24T18:05:04+00:00Added an answer on May 24, 2026 at 6:05 pm

    I solved the problem, and got the AJAX based element to maintain state. Here’s my jQuery code:

    $('#before_category_select').loadPage();
    
    jQuery.fn.loadPage = function(){
    if($("#new-post-area").length > 0){
        $.getJSON('/home/cat_select_state.json', function(data){
            $.get('/posts/update_sub_cat/' + data.cat_state, function(data1){
               $("#sub-category-select").html(data1);
            })
        });
       }
    }
    

    The controller action that I call to get the state of the select elements, which is stored as session variables at the time of page submit:

      def cat_select_state
        @cat_session = {:cat_state => session[:new_post_category], :sub_cat_state => session[:new_post_sub_category]}
        respond_to do |format|
          format.json {render :json => @cat_session}
        end
      end
    

    And finally, I used a default values for the select boxes, which are stored as session variables. If the session variable is null, the default value is the prompt message for the select box.

    <%= collection_select :post_category, :id, @categories, :id, :name, 
                                                    options = {:prompt => "Select a category", :selected => session[:new_post_category]} %>
    

    The HTML for sub-category select element is rendered in the javascript file update_sub_cat.js.erb.

    sub-category <%= collection_select :post_sub_category, :id, @sub_categories, :id, :name, 
                                            options = {:prompt => "Select a sub-category"} %>
    

    Please suggest if you have any more improvements.

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

Sidebar

Related Questions

if i have a form with the following two elements. <select name= size=5 id=reasons
I have two select elements. Making a choice on the first select will build
On the form, I have one select and two input fields. These elements are
I have two datepickers where the user makes a selection. Then for each day
I have a form in which a user can select multiple options in a
I have a form in HTML, that has several select elements, what is the
On a form, I have two combobox wich have the same DataSource (their elements
I have a SELECT element in a form which I'm populating using AJAX when
I want to select all the elements that have the two classes a and
I have a form with a table that has two input boxes, a select

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.