-4

I am dynamically adding select elements in the webpage when a certain action is taken by the user. The problem is that as the select elements are added dynamically, the jquery is unable to make them searchable as it is applied when the document is loaded. How can I make the dynamically added select boxes searchable?

EDIT

I'm using select bootstrap for making the select element searchable.

What I am doing is making the user select multiple parts of an image using select areas and whenever an area is selected, I add a select element corresponding to that area. This is done using a custom javascript in the head of the page. However, these select elements are devoid of any styling as the are added dynamically.

The select bootstrap can make a select element searchable by using data-live-search -

<select data-live-search="true" name="category_name" class="selectpicker" >
Keshav Agarwal
  • 811
  • 1
  • 10
  • 28

2 Answers2

0

You can use a plugin like Chosen. It is very easy to use, just include the files and call it like this:

$(".my-select").chosen();

And if you want to add options dynamically, you can take a look at this answer, which explains how to do it.

Community
  • 1
  • 1
Iván Rodríguez Torres
  • 4,293
  • 3
  • 31
  • 47
0

You can search for the elements with a selector, even if you bind them dynamically:

$('option').each(function(){console.log($(this).html())});

$('select').append('<option class="option2">test 2</option>');

$('option').each(function(){console.log($(this).html())});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
  <option class="option1">test</option>
</select>
Randy
  • 9,419
  • 5
  • 39
  • 56