You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was looking for a way to set selected options using JS.
I came up with the following:
/**
* Set selected options with the given values
*
* @param arrValues
* The value of the item(s) being selected
*/
this.selectOptions = function (arrValues) {
// give options correct attributes
$selectSingle.find('option').each(function () {
var currVal = $(this).attr('value');
if ($.inArray(currVal, arrValues) > -1) {
$(this).attr({selected: 'selected', 'disabled' : 'disabled'}).data('disabled', 1);
} else {
$(this).removeAttr('selected').removeAttr('disabled').removeData('disabled');
}
});
//empty list of selected
$list.empty();
// add the new selected options
// this did not work with selected!!!
$selectSingle.find(':disabled').each(function () {
add($(this), null, false);
});
};
Don't know if it is the best way but it works.
I feel it's wrong to play around with the actual data and attributes (rather call on a function for this)
Also selecting by the selected attribute failed but was OK using the disabled attribute - weird...
Be happy to get your corrections/thoughts on this.
Thank you for creating this plugin!
The text was updated successfully, but these errors were encountered:
I was looking for a way to set selected options using JS.
I came up with the following:
Don't know if it is the best way but it works.
I feel it's wrong to play around with the actual data and attributes (rather call on a function for this)
Also selecting by the selected attribute failed but was OK using the disabled attribute - weird...
Be happy to get your corrections/thoughts on this.
Thank you for creating this plugin!
The text was updated successfully, but these errors were encountered: