Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

chosen with bootstrap 3 not working properly

I have this code which triggers a bootstrap modal and load its content via $.load(). the page I'm loading has a select element which I'm calling chosen on. Here's the code:

$('.someClick').click(function(e){
   e.preventDefault();
   x.modal('show');
   x.find('.modal-body').load('path/page.html',
function(response, status, xhr){

       if(status =="success")
        $("select[name=elementName]").chosen();

    });
});

the results I'm getting is like the following image:

Modal View

and this is my Html content:

 <select name="elementName" data-placeholder="Please work.." class="chosen-select">
        <option value="test">Hi</option>
        <option value="test2">bye</option>
 </select>
like image 400
mamdouh alramadan Avatar asked Sep 30 '13 08:09

mamdouh alramadan


2 Answers

Applying Chosen after the modal is shown should solve your problem:

$('#myModal').on('shown.bs.modal', function () {
  $('.chosen-select', this).chosen();
});

Or when Chosen was already applied, destroy and reapply:

$('#myModal').on('shown.bs.modal', function () {
  $('.chosen-select', this).chosen('destroy').chosen();
});

Fiddle here: http://jsfiddle.net/koenpunt/W6dZV/

So in your case it would probably something like:

$('.someClick').click(function(e){
   e.preventDefault();
   x.modal('show');
   x.on('shown.bs.modal', function(){
       x.find('.modal-body').load('path/page.html', function(response, status, xhr){
           if(status == "success"){
               $("select[name=elementName]").chosen();
           }
       });
   });
});

EDIT

To complement Chosen you can use the Chosen Bootstrap theme

like image 77
Koen. Avatar answered Nov 10 '22 00:11

Koen.


Try this, I digged in chosen and found out that just need to pass options with "width" property like in this, or any other width value:

$("select").chosen({width: "inherit"}) 
like image 33
lexeek Avatar answered Nov 10 '22 01:11

lexeek