Search code examples
jqueryformsshow-hide

jQuery - how to show a text box based on a value from a drop down list


When the page loads I want a input textbox to be hidden the id is :amf-input-othertitle_19

But if the the user picks a certain value("other") from a drop down list for that above text box to show so they can fill in the information on that box.

Here's the code dropdown list:

<select name="title_3" id="amf-input-title_3">
  <option value="Title" id="title_3-0">Title</option>
  <option value="Mr" id="title_3-1">Mr</option>
  <option value="Mrs" id="title_3-2">Mrs</option>
  <option value="Miss" id="title_3-3">Miss</option>
  <option value="Ms" id="title_3-4">Ms</option>
  <option value="Dr" id="title_3-5">Dr</option>
  <option value="Professor" id="title_3-6">Professor</option>
  <option value="Other" id="title_3-7">Other</option>

TextBox to hide/show:

<input type="text" class="text" 
  name="othertitle_19" id="amf-input-othertitle_19" 
  value="" placeholder="Please specify other...." 
  maxlength="255" 
  onkeyup="if (this.length>255) this.value=this.value.substr(0, 255)"
  onblur="this.value=this.value.substr(0, 255)"
/>

Solution

  • $(document).ready(function(){
        $('#amf-input-othertitle_19').hide(); //Hide on PageLoad
    
        $('#amf-input-title_3').change(function(){
          if($(this).val() == 'Other'){
             $('#amf-input-othertitle_19').show(); //show if dropdown = other
          }
          else{
            $('#amf-input-othertitle_19').hide(); //hide if changed to something else
          }
    
        });
    });