...

View Full Version : ReadOnly Checkbox?



arpan_de
05-05-2007, 10:00 PM
How do I make a checkbox readonly?

I googled for a solution but all the solutions I came across turned to be bogus like for e.g.

document.forms[0].chk1.access="readOnly"

or

document.forms[0].chk1.readonly=true

or

document.forms[0].chk1.readOnly=true

I got the above solutions in different forums but none of them worked! Why do people give incorrect answers in forums & mislead others?

As they say, if you don't have an answer to a question, it's better to keep your mouth shut & look like an idiot rather than open your mouth & remove all doubts (that you are an idiot)!

Thanks,

Arpan

Philip M
05-05-2007, 10:18 PM
What is the point of having a checkbox which is read-only? It is either checked or it isn't.
So how can it be read-only? That implies that the user cannot check it.

document.formName.checkBoxName.checked=false;

would cancel any check placed the box, but as I say, what is the point? If it cannot be changed by the user, why is it there? Surely the read-only attribute applies only to text boxes.


See: http://www.htmlcodetutorial.com/forms/_INPUT_DISABLED.html

glenngv
05-05-2007, 10:55 PM
Maybe you want a disabled checkbox instead.

document.forms[0].chk1.disabled=true;

arpan_de
05-05-2007, 11:56 PM
What is the point of having a checkbox which is read-only? It is either checked or it isn't.
So how can it be read-only? That implies that the user cannot check it.

document.formName.checkBoxName.checked=false;

would cancel any check placed the box, but as I say, what is the point? If it cannot be changed by the user, why is it there? Surely the read-only attribute applies only to text boxes.


See: http://www.htmlcodetutorial.com/forms/_INPUT_DISABLED.htmlPhilip, this is the reason why I want a readonly checkbox:

A Form has 4 checkboxes-Masters, Bachelor, Higher Secondary & Secondary. When any checkbox is checked, 3 textboxes appear just below the checked checkbox where in users have to enter the name of the college from where they completed their course, the year they passed out & the marks they had scored. When the page loads for the first time, none of the 4 checkboxes are checked & consequently none of the 4 sets of 3 textboxes are visible.

So for e.g. if a user checks the Bachelor checkbox, 3 textboxes will become visible to him under the Bachelor checkbox. Next if he checks the Secondary checkbox, another set of 3 textboxes will appear just below the Secondary checkbox. If he unchecks the Secondary checkbox, the 3 textboxes under the Secondary checkbox will become invisible.

For a user to have a Masters degree, it is obvious that he has to first appear for the Secondary exam (10th std), then Higher Secondary exam (12th std) & lastly the Bachelor course. A user cannot directly enrol himself in a college to do his Masters; he has to appear for these 3 exams prior to getting admission in some college to do his Masters (of course, exceptions are always there). So if a user has a Masters degree, he will check the Masters checkbox which means that apart from entering info in the 3 textboxes under the Masters checkbox, he also has to enter info in the rest of the 3 sets of 3 textboxes under the Bachelor, Higher Secondary & Secondary checkboxes.

So when the Masters checkbox is checked, I want the Bachelor, Higher Secondary & Secondary checkboxes to get checked as well & at the same time, ensure that the user cannot uncheck the Bachelor, Higher Secondary & Secondary checkboxes.

This is the reason why I want readonly checkbox so that users can't uncheck checked checkboxes under such circumstances.

Glen, thanks for your suggestion. Currently that's what I am doing since I couldn't manage any code to make checkboxes readonly.

Any other suggestions, friends?

Thanks to both of you,

Regards,

Arpan

glenngv
05-06-2007, 02:55 AM
If you disable the checkbox, then it will not be submitted to the server when the form is submitted. What you can do is on onclick of each of those 3 checkboxes, see if the Masters checkbox is checked. If checked, then check the corresponding checkbox. This way, those 3 checkboxes cannot be unchecked.


function checkIt(chk){
if (!chk.checked && chk.form.masters.checked) {
chk.checked=true;
alert("Cannot uncheck it.");
return;
}
...
}
...
<input type="checkbox" name="secondary" onclick="checkIt(this);" />
...
<input type="checkbox" name="masters" />



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum