Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Probably a simple form mistake 1

Status
Not open for further replies.

Robbo79

IS-IT--Management
Nov 27, 2006
21
GB
Hi,

I am trying to use JavaScript to set the 'disabled' property of a form field. The field should be disabled if a certain value is selected from another field. Here is what I have:

Code:
<html>
<head>
<title>Test Form</title>
<script language='text/javascript'>
      function enableFields() {
      if(document.submit.status0.value=2) {
      document.submit.field0.disabled=false;
      document.submit.field1.disabled=false;
      } else {
      document.submit.field0.disabled=true;
      document.submit.field1.disabled=true;
      }
      }
</script>
</head>
<body>
<form name='submit' method='post' action='something.htm'>
<select name='status0' onchange='enableFields()'>
<option value = '1'>Disabled</option>
<option value = '2'>Enabled</option>
</select>
<select name = 'field0' disabled='true'>
<option value = '1'>First option</option>
<option value = '2'>Second option</option>
</select>
<select name = 'field1' disabled='true'>
<option value = '1'>First option</option>
<option value = '2'>Second option</option>
</select>
</form>
</body>  
</html>

It looks simple enough, but Firebug throws: "enableFields is not defined"

 
[1]
>form name='submit'
Never name a form with the name "submit", you would collide with the name of method submit(). Change the name to something else and adjust the other script lines accordingly.

[2] compare (==) is not the same as assign (=)
>if(document.submit.status0.value=2) {
[tt]if(document.submit.status0.value[red]==[/red]2) {[/tt]
(with .submit to be renamed)
 
Thanks, tsuji. I have made the changes you suggested but I'm still receiving the same error:

Code:
<html>
<head>
<title>Test Form</title>
<script language='text/javascript'>
      function enableFields() {
      if(document.form1.status0.value==2) {
      document.form1.field0.disabled=false;
      document.form1.field1.disabled=false;
      } else {
      document.form1.field0.disabled=true;
      document.form1.field1.disabled=true;
      }
      }
</script>
</head>
<body>
<form name='form1' method='post' action='something.htm'>
<select name='status0' onchange='enableFields()'>
<option value = '1'>Disabled</option>
<option value = '2'>Enabled</option>
</select>
<select name = 'field0' disabled='true'>
<option value = '1'>First option</option>
<option value = '2'>Second option</option>
</select>
<select name = 'field1' disabled='true'>
<option value = '1'>First option</option>
<option value = '2'>Second option</option>
</select>
</form>
</body>  
</html>
 
><script language='text/javascript'>
[tt]<script [red]type[/red]='text/javascript'>[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top