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 on('change') fires twice and one of the times with an empty value

See original GitHub issue

Code 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:closed
  • Created 7 years ago
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

23reactions
Whipcommented, Apr 15, 2017

I had this problem with my code

$('.myClassName').on('change', function(){
  console.log($(this).val());
});

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

$('select.myClassName').on('change', function(){

or you could use an ID, I haven’t tested that.

2reactions
ekamppcommented, Nov 8, 2016

Do you have jQuery required twice? This has happened to me before if jQuery is included twice in your code base.

Read more comments on GitHub >

github_iconTop 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 >

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