How can I fade out and then set focus in jQuery?

I have a small jQuery snippet:

$('#showlink').click(function(){ 
 $('#linkwindow').show('fast');
    $('#linkwindow input').focus();
}

How do I trigger focus only after the fade ends? Sometimes this happens a little earlier, and I end up with a weird rendering error.

+3
source share
3 answers

Try this using the callback parameter .

$('#showlink').click(function(){  
  $('#linkwindow').show('fast', function() {
    $('#linkwindow input').focus();
  });
});        
+8
source

You can add a callback to the method show:

$('#showlink').click(function(){ 
    $('#linkwindow').show('fast', function() {
        $('#linkwindow input').focus();
    });
})
+5
source

Use the second callback parameter.

Sort of:

$('#linkwindow').show('fast', function() { 
    $('#linkwindow input').focus(); 
});
0
source

Source: https://habr.com/ru/post/1705426/


All Articles