Reputation: 1573
For example I have the following code:
$(".someSelector").change(function()
{
// Do something
// Call function
// console.log('test')
});
The problem is, it is being called twice: whenever I lose focus of the input field and whenever the value changes.
I want it to be called only whenever the value changes.
Is there a way to ignore focus loss on the .change other than comparing old input value with the new input value?
Upvotes: 1
Views: 2150
Reputation: 30330
You should use the input
event instead of change
.
input
is fired on each value change (including paste); change
fires on blur when the value has changed.
$(".someSelector").on('input', function() {
// Do something
});
The input
event is supported by IE9+.
Upvotes: 5
Reputation: 3588
Here's an answer from another question which I think applies to you as well:
You can bind the 'input' event to the textbox. This would fire every time the input changes, so when you paste something (even with right click), delete and type anything.
$('#myTextbox').on('input', function() {
// do something
});
If you use the change handler, this will only fire after the user deselects the input box, which may not be what you want.
Source: JQuery: detect change in input field
Upvotes: 1