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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:39:08+00:00 2026-05-16T05:39:08+00:00

I’ve seen a lot of help for dynamically adding rows or fields, but I’m

  • 0

I’ve seen a lot of help for dynamically adding rows or fields, but I’m interested in controlling fields that are dependent on one another.

For example, I have a form with 3 user inputs:

<select id="foo">
  <option value="0">No</option>
  <option value="1">Yes</option>
</select>

When #foo‘s value is set to 1, I’d like to enable #bar.

<!-- by default, bar should be disabled -->
<select id="bar">
  <option value="something_1">Something 1</option>
  <option value="something_2">Something 2</option>
  <option value="other">Other...</option>
</select>

When #bar‘s value is set to other, I’d like to enable #baz.

<!-- by default, baz should be disabled -->
<textarea id="baz"></textarea>

My Goal

I’d like to some guidance on writing a small plugin that allows easy creation of form inputs with dependencies.

I’d like short, concise syntax. Is there a way I could add html attribute helpers to aid the jQuery plugin in “automating” things?

Foreseeable Issues

  1. If #foo is set to “Yes”, then #bar is set to “other”, then #foo is set to “No”, I will want #bar‘s dependencies deactivated/hidden as well.
  2. Inputs are enabled/disabled and/or visible/hidden; Having the ability to define custom behaviors for elements would be nice.

Reinventing the Wheel

If there’s a plugin out there that does this kind of thing, let me know! I couldn’t seem to find one…

  • 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-16T05:39:08+00:00Added an answer on May 16, 2026 at 5:39 am

    A quick plugin

    (function($){
    
      $.fn.inputDependsOn = function(id, values, options){
    
        var self = this;
    
        // methods
        this.disable = function(e){
          return e.addClass(options.cssClass).attr({disabled: true}).trigger("disable");
        };
    
        this.enable = function(e){
          return e.removeClass(options.cssClass).attr({disabled: null}).trigger("enable");
        };
    
        // options
        options = $.extend({}, $.fn.dependsOn.defaults, options || {});
    
        var parent = $(id);
    
        this.each(function(){
          var child = $(this);
    
          // parent binds
          parent
            .bind("change", function(){
              var v = parent.val();
              if(parent.is(":not(:disabled)") && v in values){
                self[values[v]](child);
              }
              else {
                self[options.init](child);
              }
              child.change();
            })
            .bind("disable", function(){
              self.disable(child);
            })
          ;
        });
    
        // init
        parent.change();
        return this;
      };
    
      $.fn.dependsOn.defaults = {
        init: "disable",
        cssClass: "disabled"
      };
    
    })(jQuery);
    

    Using the HTML in the original question, goal can be accomplished with:

    $("#bar").inputDependsOn("#foo", {"1": "enabled"});
    $("#baz").inputDependsOn("#bar", {"other": "enable"});
    

    This plugin will be maintained and update here:

    github.com/macek/jquery-input-depends-on

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

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I am doing a simple coin flipping experiment for class that involves flipping a

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.