Select on('change') fires twice and one of the times with an empty value
See original GitHub issueCode example:
$(() => {
$('select').on('change', e => {
const $this = $(e.currentTarget)
console.log('value:', $this.val())
})
})
When changing the select once, the following outputs in console:
value: test-value
value:
The second time it fires, the value is simply not empty, which can break your script if you don’t add the following checker:
if ($this.val().length === 0) return
Issue Analytics
- State:
- Created 7 years ago
- Comments:12 (2 by maintainers)
Top Results From Across the Web
jQuery change event being called twice - Stack Overflow
It's a good idea to check if two event listeners have been created. If the code ran twice, or if the file was...
Read more >change event is firing twice - CodeRanch
Within an event handler, that line of code will find any element that is a child of the event target that has the...
Read more >DS1.6 - M Mode - Dropdown Box - On Select Event Fired Twice
Select a new value in the Dropdown Box. The app says "Hi". ... default value empty ... //First Time Select, Default selected item...
Read more >Change event is triggered twice (the first one with a wrong ...
Here is the problem: In the DropdownList, if you type something and then press the clear button, the change event is firing two...
Read more >DropDownListFor Firing onchange event multiple times - MSDN
your code makes no sense. you attach a javascript routine to the onchange. when it fires, you attach a jquery handler to the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I had this problem with my code
which fired twice like the OP said. One with the value and other one empty. Reason that happens is because materialize creates a div that you actually interact with and hides the select element. The div has the same class so the onChange applies to the div as well. My solution was
or you could use an ID, I haven’t tested that.
Do you have jQuery required twice? This has happened to me before if jQuery is included twice in your code base.