3

I know how to toggle (active/disabled or visible/hidden) html form input fields with JavaScript.

But if there are several input fields, I guess it is better to use a declarative solution. I mean a solution without coding methods which use long "if ... else ..." statements.

Simple example:

The customer can enter if he likes long distance running. If he enables this, an other input gets visible: "What is your preferred running distance?"

I can code (and have done many times) "if ... show ....", but I am not a JS expert.

How could this be done declarative in JS?

http://en.wikipedia.org/wiki/Declarative_programming:

In computer science, declarative programming is a programming paradigm, a style of building the structure and elements of computer programs, that expresses the logic of a computation without describing its control flow.

guettli
  • 25,042
  • 81
  • 346
  • 663

1 Answers1

1

I use this extension function (from this answer):

(function($) {
    $.fn.toggleDisabled = function(){
        return this.each(function(){
            this.disabled = !this.disabled;
        });
    };
})(jQuery);

You can do something like this:

$(".long-distance-running-checkbox").change(function(){
  $(".long-distance-running-related").toggleDisabled();
}
Community
  • 1
  • 1
Max Al Farakh
  • 4,386
  • 4
  • 29
  • 56