1

How can I trigger change event if I click on already selected option. For example I have following options in dropdown

  • Red
  • Blue
  • Green

and Blue option is selected. Now I want that if I click again on blue item then the change event should fire.

pnuts
  • 58,317
  • 11
  • 87
  • 139
Ahmar Arshad
  • 477
  • 1
  • 10
  • 22
  • Possible duplicate of [Run change event for select even when same option is reselected](https://stackoverflow.com/questions/7742739/run-change-event-for-select-even-when-same-option-is-reselected) – Immortal Blue Apr 06 '18 at 06:25

2 Answers2

1

Running the change even when the user selects the selected option is a known problem - you can read here about it.

Now, when you know how to trigger an event whenever a user selects any option, you can simply store the last used value and check if the same item is selected.

var previousValue;
$("select")
    .mouseup(function() {
        var open = $(this).data("isopen");

        if (open) {
            if (this.value === previousValue)
            {
                alert("The same select has been clicked");
            }
        }

        previousValue = this.value;
    
        $(this).data("isopen", !open);
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
  <option>Red</option>
  <option selected>Green</option>
  <option>Blue</option>
</select>
Community
  • 1
  • 1
Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101
0

You might be able to achieve this with the click event, not the change event.

If you select the same option, the change event doesn't fire because you selection didn't change.

ThiagoPXP
  • 5,362
  • 3
  • 31
  • 44