Javascript - Set Checkbox properties

Status
This thread has been Locked and is not open to further replies. The original thread starter may use the Report button to request it be reopened but anyone else with a similar issue should start a New Thread. Watch our Welcome Guide to learn how to use this site.

JiminSA

Jim
Joined
Dec 15, 2011
Messages
3,407
Why not just add disabled="disabled" to the checkbox? Or am I misconstruing your need? For my own edification, why would you want a checkbox that is inoperable?
However, to answer your question: READONLY doesn't work on checkboxes, but here's a way to make a checkbox override to a required on||off state ...
Javascript
Code:
function overrideCheckbox(element)
 {
    $('#myCheckbox').attr('checked', true); // EITHER THIS LINE which Checks it
//  $('#myCheckbox').attr('checked', false); - OR THIS LINE which Unchecks it
 }
You would then put an onchange call in your checkbox html, which would look like this
HTML:
<input type="checkbox" id="myCheckbox" onchange="overrideCheckbox(this)" .....>  <!-- Checked OR Unchecked -->
This would enforce read-only (after the event) by overriding whatever the user input.
(I suggest the onchange approach so that you can catch users who are keyboard only - as opposed to onclick, which would miss them)
 
Last edited:

computerman29642

Thread Starter
Joined
Dec 4, 2007
Messages
2,895
Why not just add disabled="disabled" to the checkbox? Or am I misconstruing your need? For my own edification, why would you want a checkbox that is inoperable?
However, to answer your question: READONLY doesn't work on checkboxes, but here's a way to make a checkbox override to a required on||off state ...
Javascript
Code:
function overrideCheckbox(element)
 {
    $('#myCheckbox').attr('checked', true); // EITHER THIS LINE which Checks it
//  $('#myCheckbox').attr('checked', false); - OR THIS LINE which Unchecks it
 }
You would then put an onchange call in your checkbox html, which would look like this
HTML:
<input type="checkbox" id="myCheckbox" onchange="overrideCheckbox(this)" .....>  <!-- Checked OR Unchecked -->
This would enforce read-only (after the event) by overriding whatever the user input.
(I suggest the onchange approach so that you can catch users who are keyboard only - as opposed to onclick, which would miss them)
If I disable the checkbox, I will not be able to pass the value to a database. The checkbox is operable but only to a few users. I want some users to be able to see that the checkbox is checked or unchecked, but not change the value.
 

computerman29642

Thread Starter
Joined
Dec 4, 2007
Messages
2,895
Is there a way to keep the user from changing the value?

I was doing some research and found something like "return false"
 

JiminSA

Jim
Joined
Dec 15, 2011
Messages
3,407
Yes, you would have to set the checkboxes to disabled and under certain circumstances use javascript to set them to enabled. (.disabled, false). Exactly how you do this will depend upon the circumstances.
We can work out the code if you could explain the circumstances in which you would want to enable disabled check boxes and a link to your html is necessary as well ...
 
Last edited:
Status
This thread has been Locked and is not open to further replies. The original thread starter may use the Report button to request it be reopened but anyone else with a similar issue should start a New Thread. Watch our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Members online

Top