Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

input change event triggered on blur

when I change the value of a text input and then blur that input it triggers the input change event again. How can it register this blur as an input change? is there a way to prevent this?

$('input').on('input change',function(){
 //do something
});
like image 464
user2014429 Avatar asked Feb 27 '15 18:02

user2014429


People also ask

What is input blur event?

The blur event fires when an element has lost focus. The event does not bubble, but the related focusout event that follows does bubble. The opposite of blur is the focus event, which fires when the element has received focus. The blur event is not cancelable.

How can blurred events be prevented?

Preventing the blur If you want to prevent the blur event from being fired, you have to do so when you are inside the mousedown event, you can do so by invoking the method preventDefault() on the event. Click the checkbox, focus input & then click the button, the textfield never loses focus now.

What is on focus and on blur?

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. Tip: The onblur event is similar to the onfocusout event.

What is the difference between onChange and Onblur?

onChange is when something within a field changes eg, you write something in a text input. onBlur is when you take focus away from a field eg, you were writing in a text input and you have clicked off it.


2 Answers

$(function(){
  $('#onchange').on('change',function(){
    alert('changed')
  });
  
   $('#onEveryLetter').on('input',function(){
    alert('onEveryLetter')
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
onchange: <input type="text" id="onchange" name="Jenish" />
<br/>
onEveryLetter: <input type="text" id="onEveryLetter" name="Jenish" />
like image 98
Jenish Rabadiya Avatar answered Oct 15 '22 23:10

Jenish Rabadiya


Simply remove the change event from your code. The change event is fired onblur, but only if something has been changed since it was last blurred.

Use:

$('input').on('input',function(){
 //do something
});
like image 44
Drazzah Avatar answered Oct 16 '22 01:10

Drazzah