If you use Bootstrap modality, this does not work:
 $('#modalID').modal('show'); $('#modalID #fieldID').focus(); 
because it takes a little to get the modality to be available for focusing ... I found that the 400 ms timeout is fast enough so that users don't suffer and are slow enough so that it always focuses on the element.
 $('#modalID').modal('show'); setTimeout(function(){ $('#modalID #fieldID').focus(); }, 400); 
And actually it would be awkward to use executable comments:
 function wait_for_modal_to_be_drawn_then( fn ) { setTimeout( fn, 400 ); } $('#modalID').modal('show'); wait_for_modal_to_draw_then( function(){ $('#modalID #fieldID').focus(); } ); 
 source share