Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can i prevent blur event from happening?

Tags:

jquery

events

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.

like image 974
Tomer Avatar asked Nov 22 '12 09:11

Tomer


People also ask

What triggers blur?

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.

When would an on blur event occur?

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.

How do you turn off Onblur event in react?

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.

What is the difference between Blur and Focusout?

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.


2 Answers

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'); });
});
like image 162
Akhil Avatar answered Oct 10 '22 04:10

Akhil


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'); 
});​
like image 29
Pragnesh Chauhan Avatar answered Oct 10 '22 04:10

Pragnesh Chauhan