Semantic-UI icon indicating copy to clipboard operation
Semantic-UI copied to clipboard

Bug/Documentation issue: Dynamically setting value for dropdown

Open illuminos opened this issue 11 years ago • 19 comments

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.

illuminos avatar Jan 26 '15 10:01 illuminos

much simpler http://jsfiddle.net/0qobkc2x/

$('#test').dropdown('set selected','B');

jlukic avatar Jan 26 '15 15:01 jlukic

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?

illuminos avatar Jan 26 '15 15:01 illuminos

You dont have to do any of that.

jlukic avatar Jan 26 '15 15:01 jlukic

#1669 http://jsfiddle.net/8vy6f91g/

Please search for issues before posting new threads.

jlukic avatar Jan 26 '15 16:01 jlukic

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/

illuminos avatar Jan 27 '15 00:01 illuminos

$('#dropdown1').dropdown('set selected',value); is anought!

michtill avatar Jul 11 '15 23:07 michtill

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.

steve-ross avatar Nov 13 '15 17:11 steve-ross

Thank you very much ujwalbharatn. You saved my day :+1:

utsavig avatar Jan 18 '16 01:01 utsavig

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!

BrianJLennon avatar Feb 02 '16 15:02 BrianJLennon

@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.

darkmakukudo avatar Feb 11 '16 09:02 darkmakukudo

@ujwalbharatn Thank you! I couldn't find it in the docs, saved my time for sure! ;)

cherrylipstick avatar Feb 19 '16 18:02 cherrylipstick

another fix

// inside ur ajax call
setTimeout(function() {
    $('#something').dropdown('set selected', data.length[0]);
}, 1);

ctf0 avatar Jun 06 '16 08:06 ctf0

I had a similar problem. I discovered how to fix it and reported it in this issue.

DanielSwain avatar Feb 24 '17 03:02 DanielSwain

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?

awaistechverx avatar Jul 03 '17 15:07 awaistechverx

('set selected', null) not works. How to select none?

seajean avatar Jun 14 '18 06:06 seajean

@seajean $('#test').dropdown('clear'); ?

prudho avatar Jun 14 '18 07:06 prudho

@prudho It works, thx! Why semantic has no docs~

seajean avatar Jun 14 '18 08:06 seajean

@seajean It has, but it takes a while to navigate it: https://semantic-ui.com/modules/dropdown.html#behavior

ColinFrick avatar Jun 14 '18 08:06 ColinFrick

$('.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.

UmeshGEM avatar Mar 21 '20 18:03 UmeshGEM