0

I have a select element and need to run two different functionality. One would be when a user change value of select, and the second when a change event is fired by trigger on the element.

As far as I searched there is mainly "change" or "onChange" event for select element for such purposes, but I was wondering how to differ these tow change events.

  • Real change, I means when select an option or the other.
  • Fake change, I mean when value of select stay same but we do a trigger change.
Naser Nikzad
  • 713
  • 12
  • 27
  • 1
    Store the old value and compare it in the handler? – Nick Aug 23 '20 at 05:06
  • Well, I like to keep this as final choice if not found a better solution, since in my case this need more coding staffs. – Naser Nikzad Aug 23 '20 at 05:09
  • 2
    Does this answer your question? [Check if event is triggered by a human](https://stackoverflow.com/questions/6692031/check-if-event-is-triggered-by-a-human) – Nick Aug 23 '20 at 05:19
  • Thanks for follow, but for me both behaviors returns isTrigger as 3 (type of is number) so still not achieved! but I continue looking deeper into that. – Naser Nikzad Aug 23 '20 at 05:42
  • Did you try `e.originalEvent`? that worked for me in a small test case. – Nick Aug 23 '20 at 06:41
  • Yeah, tested that too. same output in both cases. – Naser Nikzad Aug 23 '20 at 06:49
  • Can you edit your question with a small demo which illustrates the problem? – Nick Aug 23 '20 at 06:50
  • @Nick I found another alternative, since I am using select2 library, there is this event "select2:select" for actual change, human change. Thanks for the time! – Naser Nikzad Aug 23 '20 at 07:03
  • Ah, you didn't mention it wasn't actually a proper `select`... Anyway, I'm glad you found a solution. – Nick Aug 23 '20 at 07:04
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/220293/discussion-between-naser-nikzad-and-nick). – Naser Nikzad Aug 23 '20 at 07:05

1 Answers1

0

I ended up using "select2:select", since my select element is using select2 lib. I hope it help anyone facing this issue next time.

// Real select.
$('#myselect').on("select2:select", function(e) { 
    // My code here.
});
Naser Nikzad
  • 713
  • 12
  • 27