0

I have a select options menu. I want that jQuery/ javascript appends an input field in a specific "div content" under the menu when clicking a certain option value in it.

At the moment nothing gets triggered with the click event.

If you click in the select menu on the option value "Search Field" an input field should be created in the <div id="content"> area.

My code:

$('.inputicon').click(function() {
  $(".content").append('<input id="test">')
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select name="category6" id="category6" class="form-control">
  <option value="">Delete</option>
  <optgroup label="Search" value="iconsearch">
    <option class="inputicon " value="16">Search Field</option>
  </optgroup>
</select>

<div id="content"></div>

1
  • Menu options don't get click events. Use the change event of the dropdown. Commented Aug 13, 2021 at 1:19

1 Answer 1

1

Options don't get click events. Use the change event of the <select> and check if the value is 16.

$('#category6').change(function() {
  if (this.value == '16') {
    $("#content").append('<input id="test">')
  } else {
    $("#test").remove();
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select name="category6" id="category6" class="form-control">
  <option value="">Delete</option>
  <optgroup label="Search" value="iconsearch">
    <option class="inputicon " value="16">Search Field</option>
  </optgroup>
</select>

<div id="content"></div>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.