Disabling All Form Elements In Selected Div (edited)

javascript

Solution

A way to solve it without using a scripting library such as jQuery:

function disableFormFields(container, isDisabled) {
  var tagNames = ["INPUT", "SELECT", "TEXTAREA"];
  for (var i = 0; i < tagNames.length; i++) {
    var elems = container.getElementsByTagName(tagNames[i]);
    for (var j = 0; j < elems.length; j++) {
      elems[j].disabled = isDisabled;
    }
  }
}
<select name="selector" onchange="partiallyDisableForm(this)">
  <!-- give every option a numeric value! -->
  <option value='0'>Choose Which Div To Enable</option>
  <option value='1'>One</option>
  <option value='2'>Two</option>
  <option value='3'>Three</option>
</select>
function partiallyDisableForm(selector) {
  // don't forget to give your form the ID "bigform"
  var form = document.getElementById("bigform");
  var parts = form.getElementsByTagName("DIV");

  for (var i = 0; i < parts.length; i++) {
    var part = parts[i];
    // give your form parts the ID "formpart_1" through "formpart_3"
    if (part.id.match(/^formpart_\d+$/)) {
      // you must implement what to do if selector.value is 0
      var isDisabled = (part.id != "formpart_" + selector.value);
      disableFormFields(part, isDisabled);
    }
  }
}

Problem

I have one large form that is separated into different sections with divs. Each section is within the same form (bigform) and I need to make sure only one section is enabled/editable at a time. And if the user changes sections after entering data into one section, all data would be cleared from the old section before disabling it. The ideal way for me is to have something like this: ``` <form> <select name="selector"> <option>Choose Which Div To Enable</option> <option value='1'>One</option> <option value='2'>Two</option> <option value='3'>Three</option> </select> </form> <form name="bigform"> <div id="1"> <input type="text"> <select name="foo"> <option>bar</option> <option>bar</option> </select> </div> <div id="2"> <input type="text"> <select name="foo"> <option>bar</option> <option>bar</option> </select> </div> <div id="3"> <input type="text"> <select name="foo"> <option>bar</option> <option>bar</option> </select> </div> </form> ``` When the user selects option "Two" in the selector form, all form elements in DIVs 1 and 3 would be disabled. I've searched the web for hours but I cannot find a solution. What's the best method to achieve this? I found this code online that does "almost" what I want but not quite. It 'toggles' the form elements in the given element (el). What I'm trying to do is sort of the opposite of this. ``` <form> <select name="selector" onChange="toggleDisabled(document.getElementByID(this.value))> <option>Choose Which Div To Enable</option> <option value='1'>One</option> <option value='2'>Two</option> <option value='3'>Three</option> </select> </form> <script> function toggleDisabled(el){ try { el.disabled = el.disabled ? false : true; } catch(E){} if (el.childNodes && el.childNodes.length > 0) { for (var x = 0; x < el.childNodes.length; x++) { toggleDisabled(el.childNodes[x]); } } } </script> ```

Original source