Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

seventeen

macrumors member
Original poster
Apr 9, 2009
41
0
Denton, Tx
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!
 
Something I see, use the following for the if statements
PHP:
if(disabled===false) {
...
if(disabled===true) {
 
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...
 
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...

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); }
 
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?
 
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?

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.
 
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!
 
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...

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.