select2: Using allowClear with multi-select causes the placeholder to be added as a selection

Prerequisites

  • I have searched for similar issues in both open and closed tickets and cannot find a duplicate
  • The issue still exists against the latest master branch of Select2
  • This is not a usage question (Those should be directed to the community)
  • I have attempted to find the simplest possible steps to reproduce the issue
  • I have included a failing test as a pull request (Optional)

Steps to reproduce the issue

https://jsfiddle.net/zmtq7an8/

Expected behavior and actual behavior

When I follow those steps, I see…

  • The dropdown shows when I click the clear widget.
  • The Placeholder option is shown as selected after the process instead.
    image I was expecting…
  • The clear widget to not drop down the selection area.
  • The empty placeholder to not be selected when a new item is selected.

Environment

Browsers

  • Google Chrome
  • Mozilla Firefox
  • Internet Explorer

Operating System

  • Windows
  • Mac OS X
  • Linux
  • Mobile

Libraries

  • jQuery version: 3.2.1 and 3.1.1
  • Select2 version: 4.0.3

Isolating the problem

  • This bug happens on the examples page
  • The bug happens consistently across all tested browsers
  • This bug happens when using Select2 without other plugins
  • I can reproduce this bug in a jsbin

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 2
  • Comments: 21 (2 by maintainers)

Most upvoted comments

Hey! @vbjay try this without using empty option tag <select data-placeholder="Select a number..." multiple="multiple" style="width:221px;"> <option value="1">One</option> <option value="2">Two</option> <option value="3">Three</option> </select>

This looks like a duplicate of #5060 rather than the backspace bug of #3335

Hi, for this to be done just remove your empty option and it’ll work

BTW: On title try to put something more specific about the problem, Behaviour is Off is too broad