Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery Searchable Dropdown box

I have a searchable list, created using JQuery Plugin from: http://plugins.jquery.com/searchit/

But i want some value to be selected by default when the list box shows up first time. Even though i have 'selected' mentioned in the option tag (Rockford in ex below). This part is not working.

Pls see here sample here:

http://jsfiddle.net/QuYJD/22/

$("select").searchit( { textFieldClass: 'searchbox' } );

    Type the search text:
    <br/>
    <select id="listBox1">
      <option>Robinhood</option>
      <option selected >Rockford</option>
      <option>Rome</option>
      <option>Ronda</option>
      <option>Rondon</option>
      <option>Rondonopolis</option>
      <option>Rongelap</option>
    </select>

Any idea? Or any other solution...

like image 962
Jasper Avatar asked Jul 27 '13 17:07

Jasper


People also ask

How do I add a search option to a drop down list?

Use any element to open the dropdown menu, e.g. a <button>, <a> or <p> element. Use a container element (like <div>) to create the dropdown menu and add the dropdown links inside it. Wrap a <div> element around the button and the <div> to position the dropdown menu correctly with CSS.

What is dropdownParent?

The dropdownParent option allows you to pick an alternative element for the dropdown to be appended to: $('#mySelect2').select2({ dropdownParent: $('#myModal') }); This is useful when attempting to render Select2 correctly inside of modals and other small containers.


2 Answers

You can check the chosen plugin for jQuery. Its more user friendly and has vary nice UI.

It has the feature which you are looking for. Please go to the following link for more details

http://harvesthq.github.io/chosen/

like image 183
S. Rasel Avatar answered Sep 28 '22 01:09

S. Rasel


A kinda hacky way

Maybe you could do something like this after you init the plugin :

$(".searchbox").val($("#listBox1 :selected").val())

Because this plugin seems to be making you select into this:

<input type="textbox" id="__searchit2" class="searchbox">
<div id="__searchitWrapper2" style="display: none; vertical-align: top; overflow: hidden; border: 1px solid rgb(128, 128, 128); position: absolute;">
 <select id="listBox2" style="padding: 5px; margin: -5px -20px -5px -5px;">
  <option>Robinhood</option>
  <option>Rockford</option>
  <option selected="">Rome</option>
  <option>Ronda</option>
  <option>Rondon</option>
  <option>Rondonopolis</option>
  <option>Rongelap</option>
 </select>
</div>

Plugin change

I added another option to the plugin called selected. If you set it to true, then the text box will show your selected option. Just add this extra option :

$("select").searchit({
      textFieldClass: 'searchbox',
      selected: true
 });

Demo : http://jsfiddle.net/hungerpain/QuYJD/23/

like image 33
krishwader Avatar answered Sep 28 '22 03:09

krishwader