Som Sarkar
Som Sarkar

Reputation: 289

Enabling a text box depending upon the selected value in a drop down

Hi I have an HTML page which has a text box and a dropdown box. When the page is loading for the first time, then depending upon the value in the drop down(Selected) the text box needs to be enabled or disabled. I want to do this using javascript or jquery. I need a little help.

<table>
  <tr>
    <td class="td">
      <input type="text" size="3" name="length21" value="0" disabled="disabled"/>
    </td>
    <td class="td">
      <div class="type">
        <select name="type1" id="field_type">
          <option value="TEXT" SELECTED="">TEXT</option>
          <option value="TEXTAREA" >TEXTAREA</option>
          <option value="DROPDOWN" >DROPDOWN</option>
          <option value="DATE" >DATE</option>
          <option value="CHECKBOX" >CHECKBOX</option>
          <option value="SEPARATOR" >SEPARATOR</option>
          <option value="DATETIME" >DATETIME</option>
          <option value="HIERARCHY" >HIERARCHY</option>
        </select>
      </div>
    </td>
  </tr>
  <tr>
    <td class="td">
      <input type="text" size="3" name="length21" value="0" disabled="disabled"/>
    </td>
    <td class="td">
      <div class="type">
        <select name="type2" id="field_type">
          <option value="TEXT" >TEXT</option>
          <option value="TEXTAREA" >TEXTAREA</option>
          <option value="DROPDOWN" SELECTED="">DROPDOWN</option>
          <option value="DATE" >DATE</option>
          <option value="CHECKBOX" >CHECKBOX</option>
          <option value="SEPARATOR" >SEPARATOR</option>
          <option value="DATETIME" >DATETIME</option>
          <option value="HIERARCHY" >HIERARCHY</option>
        </select>
      </div>
    </td>
  </tr>
</table>

Now what I want is, when the page loads, for the row where the drop down is having TEXT selected, the text box will get enabled. But not for the other. I would really appreciate any kind of help.

Upvotes: 1

Views: 2608

Answers (2)

Johan Haest
Johan Haest

Reputation: 4421

Changed Scott's code a bit so that it works on your load:

$(document).ready(function() {
    $('select').each(function() {
        var comboBox = $(this);
        checkValue(comboBox);
        comboBox.on('change', function() {
            checkValue(comboBox);
        });
    });
});

function checkValue(comboBox) {
    if (comboBox.val() == "TEXT") {
        comboBox.parents('td').siblings('td').children('input').removeAttr('disabled');
    }
    else {
        comboBox.parents('td').siblings('td').children('input').attr('disabled','disabled');
    }
}​

Here's a fiddle btw: http://jsfiddle.net/BbStP/1

Upvotes: 0

user527892
user527892

Reputation:

In jQuery:

$(document).ready(function(){
  $('select').on('change',function(){
    if($('select').val() == "TEXT") {
      $(this).parents('td').siblings('td').children('input').removeAttr('disabled');
    }
  });
});

Upvotes: 2

Related Questions