jQuery, toggle button disable on checkbox change

Discussion in 'Web Design and Development' started by seventeen, Jul 6, 2009.

  1. seventeen macrumors member

    Joined:
    Apr 9, 2009
    Location:
    Denton, Tx
    #1
    hey, i have a registration form on a page where the submit button's disable attribute is set to true on pageload. In this form there is an "I accept these terms..." checkbox that must be checked for the form to go through successfully. I'm trying to set the submit button's disabled attribute to false when the checkbox is checked, and again back to true if the checkbox is checked again. This way, there is no way to submit the form if that checkbox is unchecked (atleast with javascript enabled, I have set up server side checks for all of this as well). Here is the javascript I am using:

    Code:
    $('div#register input#terms').change(function() {
    		var disabled=$('div#register  input#submit').attr('disabled');
    		if(disabled=='false') {
    			$('div#register input#submit').attr("disabled", true);
    		}
    		if(disabled=='true') {
    			$('div#register input#submit').attr("disabled", false);
    		}
    	});
    Is there anything inherently wrong with this code? I can't seem to get it to work. Maybe I am approaching this the wrong way, I still don't have too much experience with javascript/jquery.

    Let me know if you can help,

    thanks!
     
  2. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #2
    Something I see, use the following for the if statements
    PHP:
    if(disabled===false) {
    ...
    if(
    disabled===true) {
     
  3. seventeen thread starter macrumors member

    Joined:
    Apr 9, 2009
    Location:
    Denton, Tx
    #3
    I tried using "===" and still no luck... is there anything else wrong with the code? I cant think of anything else that would cause this to fail...
     
  4. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #4
    The more important part of my code was not to use quotes around true and false. Did you remove those?

    Actually, you should be able to do,
    PHP:
            if(disabled) { $('div#register input#submit').attr("disabled"false); }
            else { $(
    'div#register input#submit').attr("disabled"true); }
     
  5. seventeen thread starter macrumors member

    Joined:
    Apr 9, 2009
    Location:
    Denton, Tx
    #5
    I got it to work one way, if i get rid of the else statement... (when the button's disabled attribute is set to true on pageload, checking the checkbox changes it to false). this is good.

    it appears that the if and the else statements are both running sequentially, any idea on why this might be happening?
     
  6. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #6
    Running both the if and the else is definitely not quite right. I'd have to see how it's implemented to know for certain. If you have a URL I can take a look. I don't use jQuery, so there could have been another issue in the code that I didn't recognize. You said you the check box works to enable the submit button, but does it disable when the check box is unchecked? That would be the else case.
     
  7. seventeen thread starter macrumors member

    Joined:
    Apr 9, 2009
    Location:
    Denton, Tx
    #7
    I can't, I'm still working locally on this project... I added some debugging alert functions into the mix to see where everything was falling in time and it appears that when you check the box, the button is enabled, then immediately following, it is disabled again. (presumably, the else statement...).

    I've never seen anything like this before. eh, I suppose I could just leave it be for now. There are still server side checks for all of this...

    Thanks so much for you help!
     
  8. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #8
    A thought that just occurred to me, the function is called at a "change." It might be getting called twice during the checking of the box, which would explain why it looks like both the if and else are being called. Maybe changing "change" to "click" would help. Just a thought.
     
  9. seventeen thread starter macrumors member

    Joined:
    Apr 9, 2009
    Location:
    Denton, Tx
    #9
    I'll try that out when I get back to work later today.
     

Share This Page