Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to disable a bunch of form elements at once?

I'd like to disable a section of HTML form elements depending on some conditions. This seems to be the ideal way to do that:

<fieldset disabled>
    <input value="one" />
    <input value="two" />
</fieldset>

Now, those two inputs are disabled. However, this seems to be totally borked on IE8. The inputs appear disabled but I can still type in them.

Fiddle (Not as if JsFiddle actually works in IE8)

Is there a cross-browser solution for this problem, without adding disabled to every form element (which would complicate my script). I could probably do something tricky like select the <fieldset> in jQuery, then .each() through all the form elements and disable them - however, I'm actually setting the disabled attribute using a Knockout binding so there's really no place to add such code. My last resort is to use a custom Knockout binding that disables all the children too, but le sigh.

like image 215
Mike Christensen Avatar asked Mar 26 '13 20:03

Mike Christensen


People also ask

How do I disable all elements in a form?

As of jQuery 1.6 you should use prop instead: $("#target :input"). prop("disabled", true); To disable all form elements inside 'target'.

How do I turn off whole form in react?

You should insert your form inside an element <fieldset disabled="disabled"> . This will make the whole form disabled. Shouldn't it instead be "You should insert a <fieldset disabled="disabled"> element INSIDE your form"?

How do I disable Fieldset?

When present, it specifies that a group of related form elements (a fieldset) should be disabled. A disabled fieldset is unusable and un-clickable. The disabled attribute can be set to keep a user from using the fields until some other condition has been met (like selecting a checkbox, etc.).

How do you make a form Disabled in HTML?

The disabled attribute can be set to keep a user from using the <input> element until some other condition has been met (like selecting a checkbox, etc.). Then, a JavaScript could remove the disabled value, and make the <input> element usable. Tip: Disabled <input> elements in a form will not be submitted!


2 Answers

Ok, I've come up with a Knockout.js specific implementation that hopefully will help some other people in the same boat. This solution could probably be adapted for other solutions and platforms with a little effort.

First, I created a Knockout binding:

ko.bindingHandlers.allowEdit = {
   init: function(element, valueAccessor)
   {
      if(!valueAccessor())
      {
         element.disabled = true;
         element.readOnly = true;

         if(element.tagName === 'FIELDSET')
         {
            $(':input', element).attr('disabled', 'disabled');
         }
      }
   }
};

Note, you'd have to implement the update method too if you wanted to allow changes to this binding. I didn't have this requirement.

You could then use the binding as such:

<fieldset data-bind="allowEdit: someExpression">
   <input value="One" />
   <input value="Two" />
</fieldset>
like image 79
Mike Christensen Avatar answered Sep 18 '22 08:09

Mike Christensen


In short: No. The reason behind this is because the lack of support in IE8 and the disabled attribute on the fieldset element.

Source

In IE7 and IE8, the attribute only disables form elements in the < legend >.

I'm afraid you should look for a custom solution like the answers from other users / your own custom binding.

like image 44
MarcoK Avatar answered Sep 18 '22 08:09

MarcoK