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.

Select event is not trigger if clicked on selected item

See original GitHub issue

Hello,

I searched whole Internet and I didn’t find any solution so I decided to make a new issue.

Here is example: https://codepen.io/FilipK/pen/xYWrYR

I’m using select2:select event and its triggers everytime when you select new value (that’s obv) but it doesn’t if selecting value is the same. In this example click on option with red border, close alert and then try to select this “red option” again. Event won’t start. How can I trigger it again? I tried use select2:close event but there is a problem. If we click outside select2 event triggers too and it’s not ok for me. I tried attach new event directly on select2 dropdown but it doesn’t work because you blocked it (I used $(document).on('click', '.select2-results__option', function(){});)

Any ideas?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:22

github_iconTop GitHub Comments

6reactions
scarabdesigncommented, Mar 1, 2018

Ok, may I present a more hacky solution:

var prevselection = null;
 $('select')
    .select2({
         minimumResultsForSearch: Infinity, 
         templateResult: formatState,
         templateSelection: formatState
   })
   .on('select2:opening', function(event){
      prevselection = $(event.target).find(':selected');
      $('select').val(null);
   })
   .on('select2:select', function (event) {

      var _selection = $(event.target).find(':selected');
      var available = _selection.data('available');

      if(available === false){
         alert('ok')
      }
   }).on('select2:closing', function (event) {
      if(prevselection != null && $(this).val() == null){
         $(this).val($(prevselection).val())
      }
   });
4reactions
RomanBurunkovcommented, Jan 15, 2020

Why this was closed? It’s still the issue in the latest 4.0.12

Read more comments on GitHub >

github_iconTop Results From Across the Web

Trigger change event if I click on already selected option
Show activity on this post. Running the change even when the user selects the selected option is a known problem - you can...
Read more >
Events | Select2 - The jQuery replacement for select boxes
Select2 will trigger a few different events when different actions are taken using the component, allowing you to add custom hooks and perform...
Read more >
SCR19: Using an onchange event on a select element ...
Navigate to the trigger select element, navigate through the options but do not change the value. Check that the matching option values are...
Read more >
.trigger() | jQuery API Documentation
trigger () with an event name matches the name of a property on the object, prefixed by on (e.g. triggering click on window...
Read more >
HTMLElement: change event - Web APIs | MDN
HTMLElement: change event · When a <input type="checkbox"> element is checked or unchecked (by clicking or using the keyboard); · When a <input ......
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