Disable input fields based on selection from drop down

Posted by Thomas on Stack Overflow See other posts from Stack Overflow or by Thomas
Published on 2010-03-29T03:04:13Z Indexed on 2010/03/29 3:13 UTC
Read the original article Hit count: 524

Filed under:
|
|

I have a drop down box and some text input fields below it. Based on which item from the drop down menu the user selects, I would like to disable some of the fields. I think I am failing to target the input fields correctly but I can't figure out what the problem is:

Here is the script I have gotten so far:

$(document).ready(function(){
var customfield = $('#customfields-tf-19-tf');
var customfield1 = $('#customfields-tf-20-tf');
var customfield2 = $('#customfields-tf-13-tf');


$(function() {
  var call_table = {
    'Condominium': function() {
     customfield.attr("disabled");     
     },
    'Co-Op': function() {
     customfield1.attr("disabled");
     },
    'Condop': function() {
     customfield2.attr("disabled");
     }
  };

  $('#customfields-s-18-s').change(function() {
    call_table[this.value]();
  });

});

});

And the layout for my form:

<td width="260" class="left">
<label for="customfields-s-18-s">Ownership (Required):</label>
</td>
<td class="right">
<select name="customfields-s-18-s" class="dropdown" id="customfields-s-18-s" size="" >
<option value="Condominium"> Condominium</option>
<option value="Co-Op"> Co-Op</option>
<option value="Condop"> Condop</option>
</select>
</td>
</tr> 



<tr>
<td width="260" class="left">


  <label for="customfields-tf-19-tf">Maintenance:</label>
</td>
<td class="right">
  <input type="text" title="Maintenance" class="textInput" name="customfields-tf-19-tf" id="customfields-tf-19-tf"  size="40"/>
</td>

</tr>


<tr id="newsletter_topics">
<td width="260" class="left">

  <label for="customfields-tf-20-tf">Taxes:</label>
</td>
<td class="right">
  <input type="text" title="Taxes" class="textInput" name="customfields-tf-20-tf" id="customfields-tf-20-tf"  size="40" />
</td>
</tr>


<tr>
<td width="260" class="left">

  <label for="customfields-tf-13-tf" class="required">Tax Deductibility:</label>
  </td>
<td class="right">
  <input type="text" title="Tax Deductibility" class="textInput" name="customfields-tf-13-tf" id="customfields-tf-13-tf" size="40" />
 </td>
 </tr>

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about html