www.webdeveloper.com
Results 1 to 3 of 3

Thread: Enable a disabled checkbox (with javascript)

  1. #1
    Join Date
    Jan 2007
    Posts
    10

    Enable a disabled checkbox (with javascript)

    I've found a different solution to get me where I need to be, but this problem is still plaguing me. I have a checkbox that I want to flip between disabled and enabled, but I can't do it in the code behind, and there will be no post back between the time that I need the state to change.

    I realize the issue w/.Net that it wraps a <span> tag around the CB element if the element is set to disabled to begin with, but that does not apply here. It gets *set* to disabled with javascript. However, just to isolate the incident even further, I pulled the code from a .Net environment, stripped everything down to the basics, and tried on a barebones page. I still can't get it!

    The thing is, I've been all over the net and I've seen dozens of posts of people w/this same problem who all seem to get it to work. I've used their code and still can't get it to work.

    So here's what I have for very very basic code:

    Code:
    <html>
    <head>
    <script language="javascript">
    function flipCB(cbID, state)
    {
    	var cb = document.getElementById(cbID);
    	if(cb != null)
    	{ 
    		cb.disabled = state;
    		alert(cb.disabled + "; should be = " + state);
    	}
    }
    </script>
    </head>
    <body>
    <form>
    	<input type="checkbox" id="cb1" /><br /><br />
    	<input type="button" value="HideCb" onClick="flipCB('cb1', 'true');" />
    	<input type="button" value="ShowCb" onClick="flipCB('cb1', 'false');" />
    	
    </form>
    </body>
    </html>
    I can get the HideCb to disable the checkbox, but I cannot get the ShowCb to enable it! The alert confirms that the code is executing in the correct path, but it just doesn't seem to affect it at all.

    I've tried this is FF3 and IE7.

    If someone can point out why I'm a moron, it would help me sleep tonight lol .

    EDIT: and yes, I used the search feature here, but did not find anyone who hadn't solved the problem with code that worked for me.
    Last edited by Phrozt; 06-30-2009 at 03:50 PM.

  2. #2
    Join Date
    Oct 2006
    Posts
    93
    Try removing the tick marks [ ' ] from around the true and false in your flipCB call

    Code:
    onClick="flipCB('cb1', true);
    Also, when you check if document.getElementById(cbID) is valid you can just do....

    Code:
    if (cb) {
    
    }

  3. #3
    Join Date
    Jan 2007
    Posts
    10
    Wow!! That was it!! Haha... I knew it would be something ridiculously simple. And thanks for the tip about checking for nulls. Clearly js is not my forte .

    So why in the world would it recognize 'true' as valid, but not 'false'???

    Ah well.. don't want to get my head started on that.

    Thanks again for the answer!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles