Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Dec 2004
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Form field enabled/disabled - why doesn't this work?

    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!

    Code:
    <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 :-)
    Last edited by gerb; 05-30-2006 at 11:31 PM.

  • #2
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Perhaps you want
    Code:
    if (form.plan.options[4].selected) { 
    form.otherfeedback.disabled = true;
    to be
    Code:
    if (form.plan.options[4].selected) { 
    form.otherfeedback.disabled = false;
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
    Java != JavaScript && JScript != JavaScript
    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

  • #3
    New Coder
    Join Date
    Dec 2004
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hmm. Fixed it! Just a minor mistake - this works. Hope this helps someone...

    Code:
    <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>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •