Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nafeeur10's avatar

Select2 problem

I am using Select2 for my Select-Option. But I am facing a strange problem!!

https://gifyu.com/image/IAy4

Please see this gif.

While I clicking outside without selecting an option then the select2 search bar and results are remaining!!!

What is the problem?

My Code:

<div class="form-group">
      <label for="country" class="countrySelectLabel">Country</label>
      <select name="country" style="width: 100%!important;">
          <option value="US" selected>USA</option>
      </select>
</div>

Initialisation Code:

$("[name='country']").select2({
    placeholder: "Select a country",
    templateResult: formatCountry,
    data: isoCountries,
    templateSelection: selectedCountry,
});
0 likes
4 replies
gzai's avatar
gzai
Best Answer
Level 3

if you have logic when click outside your dropdown menu, add this :

if ( your logic click outside menu ) {

	$("[name='country']").select2('close');

}
1 like
nafeeur10's avatar

@gzai,

Your solution helped me. Thanks

$(window).click(function() {
	$("[name='country']").select2('close');
});
dingomall's avatar

I had this same error while developing my website. and I could able to follow this.

This solution had helped me. I tried to apply this on my website

Please or to participate in this conversation.