Dima Dima - 5 months ago 8
jQuery Question

How to insert a field after check the value of a field

I am searching for a way to insert one or more fields into HMTL after checking the value from a other field. Here is my HTML:

<select id="status" name="status">
<option value="1" style="background-color: #cdeb8e">freigegeben</option>
<option value="2" style="background-color: red">gesperrt</option>
<option value="3" style="background-color: yellow">Rückfrage erforderlich</option>
<option value="4" style="background-color: yellow">Muster</option>
</select>


if
status > 1
I want to add
<input type="text" />
after the
select
. How can I do that?

Kld Kld
Answer

You can check after the select changes and show/hide the input according to the selected value.

$('#status').change(function() {
  if ($(this).val() > 1) {
    $('#myInput').show();
  } else {
    $('#myInput').hide();
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<select id="status" name="status">
  <option value="1" style="background-color:#cdeb8e">freigegeben</option>
  <option value="2" style="background-color:red">gesperrt</option>
  <option value="3" style="background-color:yellow">Rückfrage erforderlich</option>
  <option value="4" style="background-color:yellow">Muster</option>
</select>

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