Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

making the textbox readonly

 
Chuck Meduri
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 360
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 360
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This could work:
function Enable() {
document.form1.text1.readOnly = false;
}
function Disable() {
document.form1.text1.readOnly = true;
}
 
Dave O'Donnell
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 360
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strange ! It works on my machine. I have IE5.
 
Chuck Meduri
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
function blur(element) {
element.blur;
}
...
<input type="Text" name="input1" value="" size="20" onfocus="javascript:blur(this);">
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You pulled up a post from 2001....look at the date!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic