Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Sep 2002
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question ReadOnly Checkbox?

    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

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,035
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    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/form..._DISABLED.html
    Last edited by Philip M; 05-05-2007 at 10:33 PM.

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,048
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Maybe you want a disabled checkbox instead.
    Code:
    document.forms[0].chk1.disabled=true;

  • #4
    New Coder
    Join Date
    Sep 2002
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    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/form..._DISABLED.html
    Philip, 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

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,048
    Thanks
    0
    Thanked 251 Times in 247 Posts
    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.

    Code:
    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" />


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •