pbauerochse / searchable-option-list

a jquery plugin for a searchable optionlist
MIT License
79 stars 54 forks source link

How to select/unselect options programatically. #21

Closed suvera closed 8 years ago

suvera commented 8 years ago

Look like there is no API/Method to select/unselect options programatically.

pbauerochse commented 8 years ago

You are right, right now this is not possible. The only way to really do this would be to select by value but hypothetically it is possible to have more than one option with the same value. What exactly is your use-case for this? By which criteria would you like to select an entry?

Boukhames commented 8 years ago

$('#divcontainerid .sol-selection-container').find('input:checkbox[value=yourvalue]:not([disabled], :checked)').prop('checked', true).trigger('change', true);

vanessaYessa commented 8 years ago

I need to select certain values by default before showing a popup. This popup has a dynamic form, and I don't wann acreate an ajax just to show the select with proper values. Even though I have already the values

pbauerochse commented 8 years ago

Hi @yessa26

is Boukames solution not working for you?

vanessaYessa commented 8 years ago

Hi @pbauerochse

I don't know how to set an Id (to be honest) for the container so I used JS,

/associates is my array with selected values/ var arrayAssociated = document.getElementsByName("associated[]"); for (var i=0; i<associates.length;i++) { for (var j=0; j<arrayAssociated.length;j++){ if(arrayAssociated[j].value == associates[i]) arrayAssociated[j].click(); } }

I used the click event cuz even thought I tried set them as selected, the options were selected on the list but didn't show them on the top of the select. And changing the source didn't work either.

But now it works ')