PDA

View Full Version : runtime readonly setting of textbox


tttony
03-26-2001, 05:20 PM
Hi all,
I am trying to find out how to set the readonly
attribute of a textbox at runtime, based on a
value of a radio group...


tia

take care
tony

Dr. Web
03-26-2001, 08:30 PM
This example uses buttons to toggle the readonly attribute...but could be easily changed to radio buttons.



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
<title>Untitled</title>
<script Language="javascript">
function skip(){ this.blur(); }
function disable_all(){
document.the.text1.disabled="true";
document.the.text2.disabled="true";
document.the.text3.disabled="true";
document.the.text4.disabled="true";
document.the.text5.disabled="true";
document.the.text6.disabled="true";
document.the.text1.onfocus=skip; void(0);
document.the.text2.onfocus=skip; void(0);
document.the.text3.onfocus=skip; void(0);
document.the.text4.onfocus=skip; void(0);
document.the.text5.onfocus=skip; void(0);
document.the.text6.onfocus=skip; void(0);
}

function enable_all(){
document.the.text1.disabled="";
document.the.text2.disabled="";
document.the.text3.disabled="";
document.the.text4.disabled="";
document.the.text5.disabled="";
document.the.text6.disabled="";
document.the.text1.onfocus=null; void(0);
document.the.text2.onfocus=null; void(0);
document.the.text3.onfocus=null; void(0);
document.the.text4.onfocus=null; void(0);
document.the.text5.onfocus=null; void(0);
document.the.text6.onfocus=null; void(0);
}

function disable_group_one(){
document.the.text1.disabled="true";
document.the.text2.disabled="true";
document.the.text3.disabled="true";
document.the.text4.disabled="";
document.the.text5.disabled="";
document.the.text6.disabled="";
document.the.text1.onfocus=skip; void(0);
document.the.text2.onfocus=skip; void(0);
document.the.text3.onfocus=skip; void(0);
document.the.text4.onfocus=null; void(0);
document.the.text5.onfocus=null; void(0);
document.the.text6.onfocus=null; void(0);
}
function disable_group_two(){
document.the.text1.disabled="";
document.the.text2.disabled="";
document.the.text3.disabled="";
document.the.text4.disabled="true";
document.the.text5.disabled="true";
document.the.text6.disabled="true";
document.the.text1.onfocus=null; void(0);
document.the.text2.onfocus=null; void(0);
document.the.text3.onfocus=null; void(0);
document.the.text4.onfocus=skip; void(0);
document.the.text5.onfocus=skip; void(0);
document.the.text6.onfocus=skip; void(0);
}

</script>

</head>

<body onload="disable_all();">
<br><br><br><hr width="75%" size="10" noshade><br>
<script language="javascript">
function skip(){ this.blur(); }
</script>
<form name="the" action=""><h1>The example:</h1><br>

<input type="text" name="text1" value="try">1</input><BR>
<input type="text" name="text2" value="typing">2</input><BR>
<input type="text" name="text3" value="here">3</input><BR><br><br><br>


<input type="text" name="text4" value="try">4</input><BR>
<input type="text" name="text5" value="typing">5</input><BR>
<input type="text" name="text6" value="here">6</input><br>
<hr width="75%" size="10" noshade>
&nbsp;&nbsp;&nbsp;
<input type="button" name="button1" value="Enable all fields" onclick="enable_all();">
&nbsp;&nbsp;&nbsp;
<input type="button" name="button2" value="Disable all fields" onclick="disable_all();">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="button" name="button4" value="Enable fields 1-3" onclick="disable_group_two();">
&nbsp;&nbsp;&nbsp;
<input type="button" name="button3" value="Enable fields 4-6" onclick="disable_group_one();">
&nbsp;&nbsp;&nbsp;
<input type="reset" name="reset" value="Clear" onclick="disable_all();">
</form>
</body>
</html>

tttony
03-27-2001, 07:42 AM
Many thanks to you for your help!!!

This solved it perfectly!!!


thank you again!!

take care
tony

tttony
03-27-2001, 07:44 AM
Hi all,

Here is the actual implemented code...


function skip()
{
this.blur();
}

function getRadioValue (radioButtonOrGroup)
{
var value = null;
if (radioButtonOrGroup.length)
{
for (var b = 0; b < radioButtonOrGroup.length; b++)
if (radioButtonOrGroup[b].checked)
value = radioButtonOrGroup[b].value;
}
else if (radioButtonOrGroup.checked)
value = radioButtonOrGroup.value;
return value;
}


function enable_email_fields()
{
var holder;
holder = getRadioValue(document.form1.internet);
if (holder == "NO")
{
document.form1.email1.value = "n/a";
document.form1.email2.value = "n/a";
document.form1.email3.value = "n/a";
document.form1.email4.value = "n/a";
document.form1.email5.value = "n/a";
document.form1.email1.onfocus = skip;void(0);
document.form1.email2.onfocus = skip;void(0);
document.form1.email3.onfocus = skip;void(0);
document.form1.email4.onfocus = skip;void(0);
document.form1.email5.onfocus = skip;void(0);
}
else
{
document.form1.email1.value = "";
document.form1.email2.value = "";
document.form1.email3.value = "";
document.form1.email4.value = "";
document.form1.email5.value = "";
document.form1.email1.onfocus = null;void(0);
document.form1.email2.onfocus = null;void(0);
document.form1.email3.onfocus = null;void(0);
document.form1.email4.onfocus = null;void(0);
document.form1.email5.onfocus = null;void(0);
}
}



Thanks again for all your help


take care
tony

Dr. Web
03-27-2001, 01:23 PM
Glad to be of help!