question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Setting the selected value initially and doing a search, shows empty "currently selected"

See original GitHub issue

I have set the selected value on the select picker:

$('#myselect').selectpicker('val', '1');

I can see the correct option selected by default (though in the DOM the correspondent select option doesn’t have the selected attribute).

Then I click on the select and to a search via AJAX, and the result says “Currently selected” showing no value, even though I didn’t click on any result so I expect “Currently selected” to be the one initially set.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:2
  • Comments:9

github_iconTop GitHub Comments

3reactions
outaTiMEcommented, Mar 18, 2021

thanks @calumshep, i made some adjustements to your code:

$.fn.ajaxSelectPickerRefresh = function () {
  return this.each(function () {
    var picker = $(this).data('AjaxBootstrapSelect');
    if (!picker) {
      return;
    }
    var selected = [];
    var selectValues = picker.$element.find('option:selected');
    for (var i = 0; i < selectValues.length; i++) {
      selected.push({
        value: selectValues[i].value,
        text: selectValues[i].text,
        'class': '',
        data: {},
        preserved: picker.options.preserveSelected,
        selected: true
      });
    }
    picker.list.selected = selected;
    picker.list.replaceOptions(selected);
    // clear status text
    picker.list.setStatus();
  });
};
1reaction
demetris-manikascommented, Aug 24, 2020

@calumshep I found that calling picker.list.replaceOptions(selected); after picker.list.selected = selected;

did solve my problem.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Warning: Use the 'defaultValue' or 'value' props on <select ...
I want to display that dropdown and make that option a default value which was selected by that user last time. I am...
Read more >
Solved: Dropdown default value - set to blank value? - Page 2
Solved: I have an edit screen with two dropdowns controls. The first dropdown is Client and the sceond is ClientProject.
Read more >
How to set the default value for an HTML <select> element
The default value of the select element can be set by using the 'selected' attribute on the required option. This is a boolean...
Read more >
.val() | jQuery API Documentation
Get the current value of the first element in the set of matched elements or ... As of jQuery 3.0, if no options...
Read more >
Forms in HTML documents - W3C
The control's "current value" is first set to the initial value. ... The INPUT element is used to create a file select control....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found