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.

1 Answer 1

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();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.