Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder mOrloff's Avatar
    Join Date
    Nov 2008
    Location
    The Great Pacific NW, USA
    Posts
    424
    Thanks
    8
    Thanked 6 Times in 6 Posts

    How do I make an ENABLE function out of this working DISABLE function ??

    I found this script, and it works great:
    Code:
    <script type="text/javascript">
      function disable(element)
      {
        var input = document.getElementById(element).getElementsByTagName("input");
        for(var i = 0; i < input.length; i++)
        {
          input[i].setAttribute("disabled","true");
        }
      }
    </script>
    I tried to make the inverse by simply reversing the setAttribute() like so:
    Code:
    <script type="text/javascript">
      function enable(element)
      {
        var input = document.getElementById(element).getElementsByTagName("input");
        for(var i = 0; i < input.length; i++)
        {
          input[i].setAttribute("disabled","false");
        }
      }
    </script>
    But that didn't do it.
    Can someone show me why, and how to fix it?

    Here's the sample form which I'm trying to test it on:
    Code:
    <form>
      <input type="radio" name="test" onclick="disable('D1')" /> disable<br/>
      <input type="radio" name="test" onclick="enable('D1')" /> enable<br/>
      <fieldset id="D1">
        <input class="" type="text" value="test value1" /><input class="" type="text" value="test value2" /><br/>
        <input class="" type="text" value="test value3" /><input class="" type="text" value="test value4" /><br/>
        <input class="" type="text" value="test value5" /><input class="" type="text" value="test value6" /><br/>
      </fieldset>
    </form>
    Edit:
    The ultimate goal which I'm working toward now (step by step =) is to have a form more like:
    Code:
    <form>
      <input type="radio" name="test" onclick="disable('D1')" />
      <fieldset id="D1">
        <input class="" type="text" value="test value1" /><input class="" type="text" value="test value2" />
      </fieldset>
      <input type="radio" name="test" onclick="disable('D2')" />
      <fieldset id="D2">
        <input class="" type="text" value="test value3" /><input class="" type="text" value="test value4" />
      </fieldset>
      <input type="radio" name="test" onclick="disable('D3')" />
      <fieldset id="D3">
        <input class="" type="text" value="test value5" /><input class="" type="text" value="test value6" />
      </fieldset>
    </form>
    And have the fieldsets enable and disable according the selection of the radio buttons.

    Also, the fieldsets (and their ID's) will be dynamically generated via PHP


    Thanks-a-bunch,
    ~ Mo
    Last edited by mOrloff; 08-20-2010 at 04:31 AM.

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    // There are two ways to do it, but setting the disabled attribute to the string 'false' won't work.
    // Disabled is one of those boolean attributes that is defined as true in html if the attribute exists at all, even without a value.

    Code:
    function enable(element){
    	var input= document.getElementById(element).getElementsByTagName("input");
    	for(var i= 0; i < input.length; i++){
    		input[i].removeAttribute("disabled");
    	}
    }

    Code:
    function enable(element){
    	var input= document.getElementById(element).getElementsByTagName("input");
    	for(var i= 0; i < input.length; i++){
    		input[i].disabled=false;
    	}
    }
    Last edited by mrhoo; 08-20-2010 at 05:03 AM.

  • #3
    Regular Coder mOrloff's Avatar
    Join Date
    Nov 2008
    Location
    The Great Pacific NW, USA
    Posts
    424
    Thanks
    8
    Thanked 6 Times in 6 Posts
    Ahah!
    Thanks.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •