Causing an input to appear only when an option(select) is chosen

Asked

Viewed 3,918 times

2

I have a registration form, with a Selected, but one of the options of this Selected when selected should appear an input. Example:

<select>
    <option>value1</option>
    <option>value2</option>
</select>

<input type="text" style="display:none" />

when option 2. appeared input(display:block)

Does anyone know how to do that? Thank you

1 answer

5


First, give a id to his select so that we can work with it in javascript.

Then you can put your input within a div and display or hide it according to the selected option.

$(document).ready(function() {
  $('#inputOculto').hide();
  $('#mySelect').change(function() {
    if ($('#mySelect').val() == 'value2') {
      $('#inputOculto').show();
    } else {
      $('#inputOculto').hide();
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<select id="mySelect">
  <option>value1</option>
  <option>value2</option>
</select>
<div id="inputOculto">
  <input type="text" />
</div>

  • Right buddy! Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.