Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Making Textbox Read-Only Depending On Checkbox 1

Status
Not open for further replies.

arpan

Programmer
Oct 16, 2002
336
IN
An HTML Form has one textbox & one checkbox. I want that if the checkbox is checked, the textbox should be made read-only but if the checkbox is unchecked, the textbox should be made editable. How do I do this using JavaScript?

Actually the textbox gets populated with a value from the database (for which I am using ASP). If the value is 12:00 AM, then the textbox should be read-only & the checkbox should be checked. If the database value is something other than 12:00 AM, then the textbox should be made editable & the checkbox should be unchecked. Next if the user again checks the checkbox, then again the textbox should be made read-only with whatever value that has been entered by the user. I can take care of the comparisons of the database value with 12:00 AM & all those stuff;that's not a problem.I just want the JavaScript code which will make the textbox read-only or make it editable depending on the state of the checkbox.

Thanks,

Arpan
 
I'm pretty sure you could add an "onChange=freeze('textInput',this.checked);" to the checkBox and use the following function...
Code:
function freeze(what,how) {
document.getElementById(what).disabled = how;
}
... or maybe ...
Code:
document.getElementById(what).readOnly = how;
...take a gander at:


for more info on Disabled versus Readonly... interesting.
 
Hello Mr3Putt,

Thanks for your suggestion. I had been to the URL you have given. The article on the differences between DISABLED & READONLY is indeed interesting but it errorneously states that DISABLED does not let the user highlight the text for copying. DISABLED does let the user to highlight the text for copying.

Regards,

Arpan
 
BUT... with Checkboxes set to "readonly", you can still check and uncheck them... but your changes won't stick when the form is submitted.

If a checkbox is "disabled", then you can't click it at all.

Depending on your browser/version, you may or may not be able to select/copy text from a "disabled" text-input.

So, I would set TEXT inputs to "readonly", and Checkboxes to "disabled"... a niggling detail, but that's just me.

Good luck with your project.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top