I have bind keyup event and blur event to one object. Enter key is working fine. But as soon as focus lost then unnecessary blur event is firing. How can i restrict blur event when i use keyup event.
Actually i need to do same operation on keyup event and blur event. User will set something either by entering or by just focus out.
Here is my example.
$('.classname').on({
keyup: function (e) {
if (e.keyCode == 13) {
//Some Code
}
},
blur: function (e) {
//Some code
}
});
Demo
var enterKeyPressed = false;
$('.classname').on({
keyup: function(e) {
if (e.keyCode == 13) {
//Some Code
enterKeyPressed = true;
console.log('Enter key');
}
},
blur: function(e) {
if (enterKeyPressed) {
//Some code
enterKeyPressed = false;
}
console.log('Blur');
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<input type="text" class="classname" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With