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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:38:20+00:00 2026-05-25T09:38:20+00:00

I have a form which has follow-up questions that should be shown only shown

  • 0

I have a form which has follow-up questions that should be shown only shown if they answer a particular other question a certain way. The way I have it set up right now works great if I start out with a blank form. However, when the user comes back to edit the form and its loading whether the checkboxes are pre-checked from the database (via php), my design is breaking down.

So I need some advice or suggestions on how to best restructure my jquery code so that it will show and hide the correct elements when the form loads.

Right now, I have a number of .change() functions for checkboxes or option buttons that cause other (or multiple) elements to hide and/or show, such as the following:

$("#elt_prev_divorced").change(function () {
        if ($(this).is(":checked")) { 
                $("#questions_divorced").show("slow"); 
        } else { 
                $("#questions_divorced").hide("slow"); 
        }
});

$("input").change(function () {
    var selected_item = $(this).attr('id');

switch (selected_item) {
        case "elt_married":
            $("#elt_inrel_yes").attr('checked', true);
            $("#elt_reldes_married").attr('checked', true); 
            $("#li_spouse_or_partner").show("slow");
            $("#li_marriages").show("slow");
            break;
        case ...
}

And then I have a CSS class called “initially_hidden” that is set for all items that shouldn’t be shown when the form loads if the form is blank. In my document ready function, I hide everything with that CSS tag.

(function($) {
    $(".initially_hidden").hide(); 
})(jQuery); 

After I hide those elements, however, I now need to do the equivalent of all of those .change() functions to handle the form being loaded with existing data. I know I could copy and paste my change functions into document ready to do this, but aside from being certainly horrific style were I do that, I would much prefer to minimize duplicate code. So how could I do this best/efficiently? I am somewhat new to JQuery, so I’m not sure what direction to take here. Thank you!

  • 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-25T09:38:21+00:00Added an answer on May 25, 2026 at 9:38 am

    Firstly in your css .initially_hidden can have display:none set in the stylesheet or the in-page style. jQuery will set display:block or whatever in the inline style. So I don’t see the sense of calling hide() on your elements with the class initially_hidden.

    What I would do is handle this on the server side and set initially_hidden on all elements that should not be displayed and let my css hide those elements.

    This way, all the elements are on the form that should be displayed and all that should not be displayed are hidden. If you have a checkbox checking which will make other elements visible, I would suggest you do 1 of 2 things.

    • Traverse the dom relative to your checkbox to determine which element should be displayed, hence no need to hardcode an id. This fiddle has an example of something like this.

    • Add a custom attribute to your checkbox which contains the id for the div/element you want to display:

    HTML

    <input type="checkbox" eleToShow="marriedQuestion" class="questionCheckbox" />
    <input type="checkbox" eleToShow="divorcedQuestion" class="questionCheckbox" />
    <input type="checkbox" eleToShow="poligamyQuestion" class="questionCheckbox" />
    

    jQuery

    $(".questionCheckbox").change(function () {
            if ($(this).is(":checked")) { 
                    $("#"+$(this).attr("eleToShow")).show("slow"); 
            } else { 
                    $("#"+$(this).attr("eleToShow")).hide("slow"); 
            }
    });
    

    Hope this helps and makes sense.

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

Sidebar

Related Questions

I have a form which has only tab control in it. I want to
I have a form which has a Combo Box Control. I have selected the
I have WPF Form which has many buttons with the same code. Appearance of
I have a maximized form which has Combo-Box control (docked in top-right) with 500px
I have this html form which has options: <tr> <td width=30 height=35><font size=3>*List:</td> <td
I have a LWUIT Form which has 3 containers, each with its own content.
I have a form[method=get] which has a number of checkboxes all have the same
I have a winform form which has typical OK and Cancel buttons. The OK
I have web form with a panel which has form elements in side it.
I have written a simple CRUD form which has one select list. However the

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.