jQuery in Action, 2nd edition*
The moose likes HTML, CSS and JavaScript and the fly likes making the textbox readonly Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "making the textbox readonly" Watch "making the textbox readonly" New topic
Author

making the textbox readonly

Chuck Meduri
Ranch Hand

Joined: Nov 29, 2000
Posts: 48
Hi everybody,
I have a form 'form1' in which there is a textbox 'text1' and a bunch of radio buttons. When the user clicks a radio button, I invoke a javascript function. Based on the value of the radio button clicked, I need to make the text in the textbox 'text1' readonly and not editable.
How can I do this in the javascript function?
Any help could be appreciated.
Chuck
Dilip kumar
Ranch Hand

Joined: Oct 16, 2000
Posts: 360
Try this:
<HTML>
<HEAD>
<script language="Javascript">
function Enable() {
document.form1.text1.disabled = false;
}
function Disable() {
document.form1.text1.disabled = true;
}
</script>
</HEAD>
<BODY>
<form name=form1>
<input type="text" name=text1 id=text1 size=10>
<input type="radio" name="R1" id="R1" value="Enable" onClick="Enable()"> Enable
<input type="radio" name="R1" id="R1" value="Disable" onClick="Disable()"> Disable
</form>

</BODY>
</HTML>
Chuck Meduri
Ranch Hand

Joined: Nov 29, 2000
Posts: 48
Hi DilipKumar,
The solution you provided works great if you want to disable it. But I don't want to disable it. The problem with disabling the field is the information is not sent to the server. I want to make it readonly so that the text in the text box is sent to the server
.
Chuck
Dilip kumar
Ranch Hand

Joined: Oct 16, 2000
Posts: 360
This could work:
function Enable() {
document.form1.text1.readOnly = false;
}
function Disable() {
document.form1.text1.readOnly = true;
}
Dave O'Donnell
Greenhorn

Joined: Feb 12, 2001
Posts: 11
Hi Chuck,
I think your best bet is to ensure that when the textbox
gets the focus it loses the focus.
eg (not exact synthax but you get the idea)
if (myText.focus == true)myText.focus = false;
so the text won't be changed & it won't be disabled.
HTH ,
Dave
Chuck Meduri
Ranch Hand

Joined: Nov 29, 2000
Posts: 48
Hi Dilip,
I have already tried this but this does not work.
Chuck

Originally posted by Dilipkumar Kalyankar:
This could work:
function Enable() {
document.form1.text1.readOnly = false;
}
function Disable() {
document.form1.text1.readOnly = true;
}

Dilip kumar
Ranch Hand

Joined: Oct 16, 2000
Posts: 360
Strange ! It works on my machine. I have IE5.
Chuck Meduri
Ranch Hand

Joined: Nov 29, 2000
Posts: 48
It is working now. All the while I was trying
document.formName.textFieldName.readonly = true;
while it is actually
document.formName.textFieldName.readOnly = true;(javascript is case-sensitive)
Thank you very much,
Chuck
Originally posted by Dilipkumar Kalyankar:
Strange ! It works on my machine. I have IE5.

mohan_kuima
Greenhorn

Joined: Oct 29, 2001
Posts: 1
Hi,
You can disable the text box liek this also.
call blur on focus of the object!!!.
It works in all types of browsers
function aa()
{
//alert("onload");
document.main.dummy.blur();
}
</SCRIPT>
</HEAD>
<BODY BGCOLOR="#FFFFFF" ()">
<Form name="main" method="post" action="test2.jsp">
<INPUT type=" text" NAME="dummy" value="hi dummy" onFocus="aa()">
Peter Peter
Greenhorn

Joined: Nov 17, 2003
Posts: 1
function blur(element) {
element.blur;
}
...
<input type="Text" name="input1" value="" size="20" onfocus="javascript:blur(this);">
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
You pulled up a post from 2001....look at the date!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: making the textbox readonly