File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes HTML, CSS and JavaScript and the fly likes How can I disable a text input field? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "How can I disable a text input field? " Watch "How can I disable a text input field? " New topic

How can I disable a text input field?

Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I have two radio buttons and under each radio button I have few text input fields. I need to disable the text fields based on the radio option selection.
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
One problem you are going to run into is that only IE4 and above support diabling form fields, but with that said, here is how you do it in IE.
You are going to have a Javascript function that is activated on the radio button's, onMouseUp event. The function will look at the value of the radio field and then look at the disabled value of the text box. If the radio button is "Yes" for example, and the text box is disabled, you enable it, and vice versa. Here is the code:
var IE4 = (document.all) ? 1 : 0;
function enableText() {
if (!IE4) return;
var form = document.myForm;
var currentState = form.myText.disabled;
var changeState = form.myRadio.value;
if (changeState != currentState)
form.myText.disabled = changeState;
// -->
<FORM NAME="myForm">
<INPUT TYPE="radio" name="myRadio" value=true CHECKED onMouseUp="enableText()">Disable
<INPUT TYPE="radio" name="myRadio" value=false onMouseUp="enableText()">Enable
<INPUT TYPE="text" NAME="myText" SIZE="30" DISABLED>

You can get a more thourough description by following this link:
I agree. Here's the link:
subject: How can I disable a text input field?
It's not a secret anymore!