Jquery focus not working on blur

I use jquery to focus on the text field when blurring if validation fails. But it works in IE, but FF does not work. Any suggestions?

$("#inputBoxId").blur(function () { if ($(this).val() < 10) $("#inputBoxId").focus(); }); 
+6
jquery
source share
4 answers

It looks like you need to use setTimeout according to this question . Since you immediately focus on the element, you need to consider some time when the element first goes out of focus.

 $("#inputBoxId").blur(function() { if ($(this).val() < 10) { setTimeout(function() { $("#inputBoxId").focus(); }, 100); } }); 

An example of working on jsfiddle , tested on the chrome dev, firefox and IE8 channel.

+13
source share

val() will return a string, not a digit. Try converting and it should work:

 var value = parseInt($(this).val(), 10); if (isNaN(value) || value < 10) $("#inputBoxId").focus(); 

This also applies to non-numeric values.

+1
source share

$(this).val().length will also give the length

 $("#inputBoxId").blur(function () { if($(this).val().length < 10 ){ $(this).focus(); } }); 
0
source share

// Page Visibility:

  document.addEventListener('visibilitychange', function () { if (document.hidden) { // stop running expensive task } else { // page has focus, begin running task } }); 
0
source share

All Articles