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:
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>
Applying Chosen after the modal is shown should solve your problem:
Or when Chosen was already applied, destroy and reapply:
Fiddle here: http://jsfiddle.net/koenpunt/W6dZV/
So in your case it would probably something like:
EDIT
To complement Chosen you can use the Chosen Bootstrap theme
I had the same problem today but I needed a quickly solution... but firs a screenshot of my problem:
This is my problem
so I did this:
1) The problem is the "width", so I saw on console this
take a look on the console, over the "select"
2) I made a quickly solution adding a new "width". As you can see in the previous image, there is a class 'class="chosen-container chosen-container-single', so I took the class "chosen-container" to add the new "width" when I call my "Modal". Here my code:
just add one line
so, basically I added this code:
to the function that call the modal. Feel free to copy and paste this on your function :) Maybe it is not a perfect solution but it works for me and maybe for you too.
sorry for my english, but I'm learnig. I speak spanish better than english. Greetings
PD: There is my result
As described in Allow Chosen to be used in Bootstrap modal, the problem is within the
chosen.jquery.js
file. I found it on line 435 and just added the following code inside theelse
statement. check it out, it worked for me.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:
The following solution worked for me (from http://codepen.io/m-e-conroy/pen/ALsdF):
Redefine "modal" class:
Adding width to your chosen class will solve this problem.