Page 1 of 1

check box from hell

Posted: Tue Aug 05, 2008 5:20 am
by deeessay
Hi there, my problem is not php-related but javascript. I posted it here anyway hoping that you all might help me with it.


I have a 7 checkboxes, right, named cb1 thru cb7, and I added a code in javascript that sends an alert message if it detected that all checkboxes are not ticked.

here's the code:
==========================================================================================
if (document.frm.cb1.checked == false && document.frm.cb2.checked == false && document.frm.cb3.checked == false && document.frm.cb4.checked == false && document.frm.cb5.checked == false && document.frm.cb6.checked == false && document.frm.cb7.checked == false)
{
alert ("at least one checkbox must be ticked.");
return false;
}
===========================================================================================

The problem is this:

One of the checkbox is ticked but disabled... how can I tell javascript that a checkbox is ticked only disabled?

Javascript seems to treat disabled boxes as if they are unticked.


Thanks in advance for any help you people can give :)

Re: check box from hell

Posted: Tue Aug 05, 2008 8:22 am
by jayshields
http://www.google.com/search?q=javascri ... d+checkbox The first result tells you everything you need to know...

Re: check box from hell

Posted: Tue Aug 05, 2008 7:25 pm
by deeessay
Thank you for your help, Jay!

Re: check box from hell

Posted: Tue Aug 05, 2008 7:42 pm
by deeessay
Hi again, the script can now detect if a checkbox is disabled but when a checkbox is disabled and ticked, it cannot detect that the checkbox is ticked.

I added this code:

if (document.frm.cb1.disabled == false && document.frm.cb1.checked == true)
{
alert ("disabled but ticked."); return false;
}


I cannot make the condition true. the cb1 checkbox is disabled and checked:

<input type='checkbox' name='cb1' value='1' disabled checked>

Is there anyway of modifying he code such that it can tell the difference between a disabled and unticked checkbox and a disabled but ticked checkbox?

Re: check box from hell

Posted: Wed Aug 06, 2008 1:44 am
by RobertGonzalez
deeessay wrote:Hi there, my problem is not php-related but javascript.
PHP - Code is the forum for PHP related questions. Miscellaneous is what we use for other programming language related questions.

Moved to miscellaneous.

Re: check box from hell

Posted: Wed Aug 06, 2008 9:18 am
by jayshields
Shouldn't this be in Client Side, not Miscellaneous?

Your if() statement that you added is incorrect for what you want. You are checking if the disabled attribute is false, meaning that it is enabled. So your if() statement is checking for an enabled and checked checkbox, not a disabled and checked checkbox.

Re: check box from hell

Posted: Wed Aug 06, 2008 10:37 am
by RobertGonzalez
Client side is design/UI related type things. The questions asked in this thread are programming related so I moved it to Miscellaneous, which is where programming questions that are not PHP related belong.