I have a simple example where i have an input
field and i put a blur
and change
event on it:
HTML
<input name="test" value=""/>
JS
$('input').change(function(e){
alert('change');
});
$('input').blur(function(e){
alert('blur');
});
Is it possible to prevent the blur
event from happening if the change
event was triggered?
One way would be to define a boolean that changes when the change event was triggered, but I don't like it, is there a better way?
Here is an example you can play with.
The blur event occurs when an element loses focus. The blur() method triggers the blur event, or attaches a function to run when a blur event occurs. Tip: This method is often used together with the focus() method.
The onblur event occurs when an object loses focus. The onblur event is most often used with form validation code (e.g. when the user leaves a form field). Tip: The onblur event is the opposite of the onfocus event.
In this situation, call event. preventDefault() on the onMouseDown event. onMouseDown will cause a blur event by default, and will not do so when preventDefault is called. Then, onClick will have a chance to be called.
The focusout event fires when an element has lost focus, after the blur event. The two events differ in that focusout bubbles, while blur does not. The opposite of focusout is the focusin event, which fires when the element has received focus.
Solved issue by this..
$('input').change(function(e){
alert('change');
e.stopImmediatePropagation();
$(this).off("blur");
});
$('input').focus(function(e){
$(this).on("blur", function(e){
e.stopImmediatePropagation();
e.preventDefault();
alert('blur'); });
});
use .off
DEMO
$('input').change(function(e){
alert('change');
e.stopImmediatePropagation();
$(this).off("blur"); //$("input[name='test']").off("blur");
});
$('input').blur(function(e){
e.preventDefault();
alert('blur');
});
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