Focus on Bootstrap modals

Today’s discovery which I’m going to post here because I know I’ll need it again:

$('body').on('shown', '.modal', function() { $('textarea,input', this).filter(':visible:first').focus(); });

Will make sure that when any modal pops up, the first input or textarea field on it will receive focus. If you’re lucky enough to be upgraded to Bootstrap 3, that shown changes to shown.bs.modal or something like that.