Bug/Documentation issue: Dynamically setting value for dropdown
Hi. I could set dropdown values dynamically before by calling
$('.ui.dropdown').dropdown('set value','A').dropdown('destroy').dropdown();
Now this code doesn't seem to work in 1.8.0. Here's my fiddle http://jsfiddle.net/z9furzn6/1/
I couldn't find documentation on this anywhere. Where can I find more information on what can be passed inside modules (other than the ones in the official documentation)? Is the source code the only place to look?
TLDR; What is the best way to change dropdown values dynamically?
Thanks for your help.
much simpler http://jsfiddle.net/0qobkc2x/
$('#test').dropdown('set selected','B');
This only works when the page loads. What if I have values coming from an ajax response and have to set the values of my dropdown accordingly? Previously I used to destroy and recreate the dropdown to reflect the changes. But it doesn't seem to happen anymore as I keep getting this error : "Dropdown: Once a select has been initialized behaviors must be called on the created ui dropdown".
For example, in the above fiddle you gave me, I am unable to do set the value to A without page reloading (via js console).
How can I achieve that?
You dont have to do any of that.
#1669 http://jsfiddle.net/8vy6f91g/
Please search for issues before posting new threads.
For those looking for the same solution (and are noobs like me)
The 'set selected' method only works on ".ui.dropdown", which can only be initialized once. So if you, like me, have thousands of dropdowns in your project..use the following method.
Initialize it once in your global script like this.
$('.ui.dropdown').dropdown();
Then when you want to change the value of a dropdown dynamically, you might have tried this
$('.ui.dropdown').dropdown('set selected','A');
But as you can clearly see, the above will set the values of all dropdowns to A. And not only that, it wont work after page load and can only be run once. So what if you have multiple dropdowns containing similar values but only need to affect one dynamically?
Try this instead.
$('.ui.dropdown').has('the class or id of the dropdown you want to affect').dropdown('set selected','A');
This will ensure only one of them will be affected. We can abstract this concept into a simpler function like below and place it in your global js file.
function changeValue(dropdownID,value){
$('.ui.dropdown').has(dropdownID).dropdown('set selected',value);
}
And pass info like this to change dropdown values dynamically.
changeValue('#dropdown1','A');
This function can be placed inside an ajax success callback and apply values accordingly.
Here is a fiddle that explains the same thing --> http://jsfiddle.net/0qobkc2x/2/
$('#dropdown1').dropdown('set selected',value); is anought!
This is a huge pain when you have a framework that changes the value of the hidden input reactively and the select doesn't update the selected item to the hidden value.
Thank you very much ujwalbharatn. You saved my day :+1:
Great post ujwalbharatn! Thank you very much.
I could not get this to work doing it as the semantic docs recommend and your solution saved me some heartache...when I eventually found it!
For the record this is the code I used in case it helps anyone else...
var priceType = this.priceType; $('.ui.dropdown').has('#priceType').dropdown('set selected', priceType);
Thank you again!
@ujwalbharatn Thanks for this. I also encountered issue when clearing a dropdown. I set dropdown value after onchange of another dropdown. I guess the state of the dropdown was changed.
If i use this syntax $('#' + targetDropdown).dropdown('set selected', value); it will not display the placeholder after clearing it.
@ujwalbharatn Thank you! I couldn't find it in the docs, saved my time for sure! ;)
another fix
// inside ur ajax call
setTimeout(function() {
$('#something').dropdown('set selected', data.length[0]);
}, 1);
I had a similar problem. I discovered how to fix it and reported it in this issue.
i am using this with angular $('#test').dropdown('set selected','B');
and getting error $apply already in progress
this is my code.
angular.element(document.querySelector('select#sortBy')).dropdown('set selected','Desc');
any one can help?
('set selected', null) not works. How to select none?
@seajean $('#test').dropdown('clear'); ?
@prudho It works, thx! Why semantic has no docs~
@seajean It has, but it takes a while to navigate it: https://semantic-ui.com/modules/dropdown.html#behavior
$('.ui.dropdown').dropdown('set selected',"value you want to set");
thanks this is the perfect solution for set the dynamic value in SEMANTIC UI dropdown.