jquery - on focusout set focus on the same cotrol -


i have written function on focusout of textbox , want check , focus on same if blank. not setting focus.

<div class="controls">     <input type="text" class="form-control"  name="inputid" id="inputid"/> </div> 
$('#inputid').focusout(function(e) {     if ($(this).val()=="") {         alert("focus onthe same")         $(this).focus();         e.preventdefault();     }     else {     } }); 

jsfiddle link: https://jsfiddle.net/sw91q0vk/

i think alert box , preventdefault causing problem, focus event not getting fired, either use settimeout or remove alert.

i have removed alert box , fiddle goes this

change in code this

$('#inputid').focusout(function(e) {   if ($(this).val()=="") {      settimeout(function(){         console.log(323);       $(this)[0].focus();     },500)     e.preventdefault();   }   }); 

now have removed alert, honest alert 2005, lets use 2016 collection throwing error, notify or something, there plenty out there.


Comments