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.

Unable to select blank option with selectOnClose=true & allowClear=true

See original GitHub issue

Unable to select blank option with selectOnClose=true & allowClear=true

Sample pen: https://codepen.io/jmalatia/pen/wvwQqXw

Sample code

<form>
   <div class="form-group>
      <label for="Order_RecdViaId">RecdViaId</label>
      <select class="form-control select2" autofocus="" id="Order_RecdViaId" name="Order.RecdViaId" >
         <option></option>
         <option value="4">Ground - UPS</option>
         <option value="5">3-Day - UPS Orange</option>
         <option value="6">2-Day - UPS Blue</option>
         <option value="7">1-Day PM - UPS Red Saver ~4:30pm</option>
         <option value="8">1-Day AM - UPS Red ~10:30am</option>
      </select>
   </div>
   <br />
   <div class="form-group">
      <label for="ctl2">Control2</label>
      <input type="text" id="ctl2" name="ctl2" class="form-control" />
   </div>
</form>

<script>
  $(document).ready(function () {
    $("select.select2").select2({
      placeholder: "Select an option...",
      selectOnClose: true,
      allowClear: true
    });
  });
</script>

select2 v4.0.10 tested on Windows 10 Chrome 77

Steps to reproduce in supplied pen:

  1. Select an option in select2
  2. Click the X to clear the option
  3. First option is selected instead of the first, blank option.

What is expected: The first blank option should be selected and/or the first blank option should be in the list so it can be selected manually.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
itsnunolemoscommented, Jan 31, 2021

+1, this bug is reported in 2016 https://github.com/select2/select2/issues/4579

0reactions
vetezeTEcommented, Feb 26, 2021

I added an additional check at the top of _handleSelectOnClose and that seems to have given me the result I’m expecting… for now.

SelectOnClose.prototype._handleSelectOnClose = function (_, params) {

    if (params && params.originalEvent == null) {
        return;
    }
    // etc...
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

default select option as blank - html - Stack Overflow
select an option -- Will be displayed by default. But if you choose an option, you will not be able to select it...
Read more >
Should a blank option always be included in <select> inputs?
If this is a required field, then the "please select" option is never valid during an edit, and therefore should not be included...
Read more >
Cannot get Custom Select List "On Change" event to work with ...
I have a custom select list that is populated by a data master, and the first record in the data master is a...
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