PDA

View Full Version : Disable/Enable Button


RobDog888
10-27-2007, 08:04 AM
I need to disable a form button until the member checks a checkbox. Since the form is created in template code I am unsure if it needs to be coded in the php fle or if it really can be done in the template code. Its late right now so maybe I am just missing something simple :)

Thanks for any help.

Analogpoint
10-27-2007, 03:49 PM
<form action="x" method="post" id="myform">
<textarea name="text"></textarea>
<label><input name="check" type="checkbox" onchange="handle_button_state();" /> Blah</label>
<input type="submit" disabled="disabled" name="sub" />
</form>
<script type="text/javascript">
function handle_button_state()
{
var el = document.getElementById('myform');
if (el)
{
if (el.check.checked)
{
el.sub.disabled = '';
}
else
{
el.sub.disabled = 'disabled';
}
}
}
</script>

RobDog888
10-27-2007, 05:03 PM
Thanks I'll try it out and let you know how it worked. :)

RobDog888
10-28-2007, 06:38 PM
<font color="DarkGreen">Hmm, no dice. Button stil enabled and checking the checkbox doesnt change anything. I verified the id of the form and the name of the checkbox and submit button. Also, the event onchange addition to the checkbox element.</font>

Analogpoint
10-29-2007, 02:41 AM
Works fine for me. Post your code and what browser you're using to test it.

RobDog888
10-29-2007, 06:32 AM
Just figured out that it doesnt work until it looses focus and requires you to check the checkbox/uncheck/recheck it in order to have it get in sync.

I have tested it on IE6 and IE7 so far with IE 6 only requiring the loss of focus one time.
<form enctype="multipart/form-data" id="upload">
'...
'...
<input type="checkbox" name="acceptterms" value="0" id="terms" onchange="handle_button_state();" />
'...
'...
<input type="submit" class="button" name="sbutton" id="{$editorid}_save" value="$vbphrase[submit]" accesskey="s" tabindex="1" />
</form>

<script type="text/javascript">
function handle_button_state()
{
var el = document.getElementById('upload');
if (el)
{
if (el.acceptterms.checked)
{
el.sbutton.disabled = '';
}
else
{
el.sbutton.disabled = 'disabled';
}
}
else
{
alert('blah');
}
}
</script>

--------------- Added 1193648996 at 1193648996 ---------------

I changed it to the onclick event and it works better and also added the onblur and onfocus but I cant get it to disable the submit button on the first time page load? I used onload but nada.

Analogpoint
10-29-2007, 02:37 PM
Have it disabled by default-- see my example.

RobDog888
10-29-2007, 04:10 PM
<font color="darkgreen">Ah, I missed this part disabled="disabled"

Thanks.</font>