Cancel onBlur event in JavaScript?

I want to configure the onBlur event for an input element that checks the value and, if it is invalid, cancels the blur and returns that input. However, returning false from onBlur does not cancel onBlur , as is done with onClick . Is there a solution for this (possibly using jQuery?)

+6
javascript jquery events
source share
3 answers

I don’t know any reliable cross-browser way to do this. Usually setting a small timeout in the onblur event and calling focus() when the timer fires.

For example:

 document.getElementById('your_input_id').onblur = function() { var self = this; setTimeout(function() { self.focus(); }, 10); } 
+7
source share

You can call focus() in the handler.
Sometimes it helps.

+4
source share

You can accomplish this using the jQuery focus() function inside the zero stopwatch. Here is an example:

 $('#my_input').bind('blur', function(event) { var $input = $(this); var is_input_valid = false; // Code to determine if input is valid // ... if (!is_input_valid) { setTimeout(function() { $input.focus(); }, 0); return false; } }); 
+2
source share

All Articles