How to add option values on silviomoreto's Bootstrap-Select

How to add option values on silviomoreto's Bootstrap-Select



I used silviomoreto's bootstrap-select but I'm having problems.
I have 2 Dropdowns (Select) a Region dropdown and Cities. Now, I have a jquery onchange function, the function will change the values in the cities dropdown depending on the region selected. Here's my code:


<select id="region" class="selectpicker"></select>
<select id="cities" class="selectpicker"></select>

$("#region").on('change', function ()
$("#cities").html('<option>city1</option><option>city2</option>');
);



The problem is I think the compatibility? When I removed the class="selectpicker" it works fine, but I can't remove it because I used it for my dropdown ui. I checked the documentation in here https://developer.snapappointments.com/bootstrap-select/ but I can't find relevant answer to my question. Thanks!




1 Answer
1



Try selectpicker('refresh').


selectpicker('refresh')



To programmatically update a select with JavaScript, first manipulate
the select, then use the refresh method to update the UI to match the
new state. This is necessary when removing or adding options, or when
disabling/enabling a select via JavaScript.



Snippet


$("#cities")
.html('<option>city1</option><option>city2</option>')
.selectpicker('refresh');



Working Demo: http://jsfiddle.net/codeandcloud/nr54vx7b/





I can not get it running for my problem here.
– user977828
Oct 5 '14 at 12:06





I checked the jsfiddle you supplied and it doesn't work either
– Tahir Khalid
Jan 15 '16 at 0:50





that was because the jsfiddle css and js links were broken. updated fiddle
– naveen
Jan 15 '16 at 20:06





Thanks Naveen.it helped me
– vision
Feb 24 '17 at 3:44





Oh man You're a legend. I was about to hit my head on wall. then see your answer you saved me
– Saad Suri
Mar 22 '17 at 1:13



Required, but never shown



Required, but never shown






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)