...

View Full Version : Form field enabled/disabled - why doesn't this work?



gerb
05-30-2006, 10:09 PM
Hi,

Basically, the text field should be disabled unless the Other (please specify) option is selected, in which case it becomes usable.

The code looks right to me, but it doesn't work. Help!



<html>
<head>
<title></title>
<script type="text/javascript">
function other() {
var form = document.form;
if (form.plan.options[4].selected) {
form.otherfeedback.disabled = true;
form.otherfeedback.focus();
} else {
form.otherfeedback.value = "";
form.otherfeedback.disabled = true;
}
}
function valid() {
var form = document.form;
var what = form.plan.value;
if (what == "" || what == "none") {
alert("No option selected!");
return false;
}
if (form.plan.options[4].selected) {
if (form.otherfeedback.value == "") {
alert("No feedback entered!");
return false;
}
}
return true;
}
</script>
</head>
<body>
<form action="feedback.cfm" method="post" id="form" name="form" onsubmit="return valid();">
<select name="plan" onchange="other()">
<option value="none">
Select Your Answer --
<option>
Potatos
</option>
<option value="More bulletins">
Onions
</option>
<option value="Don't know">
Carrots
</option>
<option value="Other">
Other (please specify)
</option>
</select>
<p>
<textarea name="otherfeedback" wrap="PHYSICAL" rows="3" cols="40" disabled></textarea>
</p>
<input type="submit" value="Submit">
</form>
</body>
</html>

Thanks in advance :-)

Kravvitz
05-30-2006, 10:35 PM
Perhaps you want

if (form.plan.options[4].selected) {
form.otherfeedback.disabled = true;
to be

if (form.plan.options[4].selected) {
form.otherfeedback.disabled = false;

gerb
05-30-2006, 10:37 PM
Hmm. Fixed it! Just a minor mistake - this works. Hope this helps someone...


<html>
<head>
<title></title>
<script type="text/javascript">
function other() {
var form = document.form;
if (form.plan[4].selected) {
form.otherfeedback.disabled = false;
form.otherfeedback.focus();
} else {
form.otherfeedback.value = "";
form.otherfeedback.disabled = true;
}
}
function valid() {
var form = document.form;
var what = form.plan.value;
if (what == "" || what == "none") {
alert("No option selected!");
return false;
}
if (form.plan[4].selected) {
if (form.otherfeedback.value == "") {
alert("No feedback entered!");
return false;
}
}
return true;
}
</script>
</head>
<body>
<form action="feedback.cfm" method="post" id="form" name="form" onsubmit="return valid();">
<select name="plan" onchange="other()">
<option value="none">
Select Your Answer --
<option>
Potatos
</option>
<option value="More bulletins">
Onions
</option>
<option value="Don't know">
Carrots
</option>
<option value="Other">
Other (please specify)
</option>
</select>
<p>
<textarea name="otherfeedback" id="otherfeedback" wrap="PHYSICAL" rows="3" cols="40" disabled></textarea>
</p>
<input type="submit" value="Submit">
</form>
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum