Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding two identical select2 forms to same page

I am trying to add two select2 multi-value select boxes to my Rails app.

The top form works fine, but the bottom one does not work.

I experimented with changing ids and adding new js code, but no success. Is there something fundamentally wrong that I'm missing?

Index.html

# This one works fine
<div class="search_genre">Genre:</div>
<div class="select2-container select2-container-multi populate">
    <select multiple="" name="e9" id="e9" style="width:300px" class="populate select2-offscreen" tabindex="-1" placeholder="Search or Select">
       <optgroup label="Popular">
           <option value="Alt">Rock</option>
           <option value="Ind">Indie</option>
           <option value="Ind">Alternative</option>
           <option value="Ind">Acoustic</option>
       </optgroup>
      </select>
</div>


# This one doesn't show a text input or the optiongroup values
<div class="search_region">Region:</div>
<div class="select2-container select2-container-multi populate">
    <select multiple="" name="e9" id="e9" style="width:300px" class="populate select2-offscreen" tabindex="-1" placeholder="Search or Select">
       <optgroup label="Local">
           <option value="PDX">Greater Portland</option>
           <option value="OR">Oregon</option>
           <option value="WA">Washington</option>
       </optgroup>
      </select>
</div>

application.js

$("#e9").select2();
like image 476
Benjamin Dunphy Avatar asked May 21 '14 17:05

Benjamin Dunphy


People also ask

How do I create a Select2 dynamically?

Select2 jQuery plugin is easy to add on the <select > element. Need to call the select2() method on the selector to initialize. If you adding select2 on a class or select element and when you add an element dynamically then select2 is not initialized on that element.

How do I add options in Select2?

New options can be added to a Select2 control programmatically by creating a new Javascript Option object and appending it to the control: var data = { id: 1, text: 'Barn owl' }; var newOption = new Option(data. text, data.id, false, false); $('#mySelect2'). append(newOption).

How do I add a class to a Select2 container?

select2 would make an other span with select2-container class right at the end of the body every time you click on the span tag and the dropdown with the options become visible, so you can use your click event and trigger a function that add your custom class to the dropdown container at the end of the body and also ...

How do I clean my Select2?

In order to clear the selection of those values which are selected using a Select2 drop down,we can use the empty() function. The dropdown can be reset using Jquery. $("#select2_example"). empty();


3 Answers

You can not have two elements with the same id. Use classes instead.

Both your selects have id="e9" instead try adding something like class="my-select" to your <select> element.

then do $('.my-select').select2();

like image 180
Ramy Nasr Avatar answered Oct 20 '22 10:10

Ramy Nasr


For each select2 in the same page you must define unique id for tag "data-select2-id".

data-select2-id="Project"

and

data-select2-id="WBSDate"
like image 22
Sergio Cerda Avatar answered Oct 20 '22 09:10

Sergio Cerda


As @Ramy Mention. I hope its works fine for everyone. If It won't work then try this. In select box you can't use same id for an example:

       <select class="form-control select2" id="select">
           <option>Select</option> 
           <option>Cycle</option> 
           <option>Horse</option> 
        </select>
        <select class="form-control select2" id="select">
           <option>Select</option> 
           <option>Cycle</option> 
           <option>Horse</option> 
        </select>

If you have same id the search box won't work. Just try to different Id Name like below.

       <select class="form-control select2" id="select">
           <option>Select</option> 
           <option>Cycle</option> 
           <option>Horse</option> 
        </select>
        <select class="form-control select2" id="select1">
           <option>Select</option> 
           <option>Cycle</option> 
           <option>Horse</option> 
        </select>

I faced this kind of problem. That's why i shared here my opinion. Thanks hope someone will get help from this.

like image 24
Arman H Avatar answered Oct 20 '22 09:10

Arman H