PDA

View Full Version : form objects


phani
09-13-2002, 02:51 PM
is there provision for making the textbox or other form objects disable making it to inactive

ShriekForth
09-13-2002, 03:53 PM
In IE there is a disable that can be used on the form elements. You could change the focus of an item to have a similar fucntionality in NS and other browsers.

<input type="text" name="itm1" onFocus="document.forms[0].itm2.focus()" value="Don't Edit Me" style="background-color: #cccccc">
<input type="text" name="itm2">

This will keep the user from editing the field at least, if they have javascript turned on.

ShriekForth

Roy Sinclair
09-13-2002, 04:07 PM
The following sample page show how it's done:


<html>
<head>
<script>
function disableField(myField)
{
myField.disabled = true
return true
}
function enableField(myField)
{
myField.disabled = false
return true
}
function onfocusField(myField)
{
if (myField.disabled)
{
myField.blur()
return false
}
return true;
}
// The above functions are generic, the following function is specific to this page
function toggleFields(myField)
{
if (myField.checked)
{
disableField(document.forms["example1"].text1)
disableField(document.forms["example1"].checkbox1)
disableField(document.forms["example1"].select1)
}
else
{
enableField(document.forms["example1"].text1)
enableField(document.forms["example1"].checkbox1)
enableField(document.forms["example1"].select1)
}
document.forms["example1"].text1
}
</script>
</head>
<body>
<form name="example1">
Disable Fields: <input type="checkbox" name="control1" onclick="toggleFields(this)" checked="checked">
<br>
Text Field: <input type="text" name="text1" onfocus="return onfocusField(this)">
<br>
Check box: <input type="checkbox" name="checkbox1" onfocus="return onfocusField(this)" onclick="return onfocusField(this)">
<br>
Select: <select name="select1" onfocus="return onfocusField(this)">
<option></option>
</select>
</form>
<script>
disableField(document.forms["example1"].text1)
disableField(document.forms["example1"].checkbox1)
disableField(document.forms["example1"].select1)
</script>
</body>
</html>

zoobie
09-13-2002, 08:13 PM
<input type="text" readonly name="textbox" value="Whatever">

whammy
09-14-2002, 07:43 AM
Or:

<input type="text" value="blah" onfocus="this.blur()" onchange="this.value=this.defaultValue" />

joh6nn
09-14-2002, 08:36 AM
uh, disable isn't IE only. it's standard.

Coral_Lover
10-02-2002, 09:25 AM
Hi, how can i disable a HTML textfield?? Is it using MyForm.TextField.readonly = true???

glenngv
10-02-2002, 09:37 AM
Originally posted by whammy
Or:

<input type="text" value="blah" onfocus="this.blur()" onchange="this.value=this.defaultValue" />


...or

<input type="text" value="blah" onfocus="this.blur()" onchange="this.value=this.defaultValue" disabled/>

or if you want to retrieve this field in the next page when submitted:

<input type="text" value="blah" onfocus="this.blur()" onchange="this.value=this.defaultValue" readonly/>

Roy Sinclair
10-02-2002, 02:46 PM
Originally posted by Coral_Lover
Hi, how can i disable a HTML textfield?? Is it using MyForm.TextField.readonly = true???

Try the sample I posted, it works in Netscape 4, IE 4+ and Gecko based browsers. If it didn't support Netscape 4 it'd be simpler but it's not that complex now.

Alekz
10-02-2002, 03:13 PM
Hi,
There's a big difference between disable and readonly
Disabled fields are not submitted to the server, readonly ones are...

Alex

whammy
10-03-2002, 01:55 AM
Yup! That's the main reason I use redundant stuff... like:

<input type="text" name="whatever" value="whatever" onfocus="this.blur()" onchange="this.value=this.defaultValue" readonly="readonly" />

and to top it off, the above code SHOULD be XHTML compliant... not to mention as far as I know it will prevent people that have javascript enabled from editing the field at all... ?