Center the first field in modal forms

I use Bootstrap from Twitter to create a modal dialog in a Marionette app.

I would like to know how best to focus the first field in modal forms without using fancy javascript.

Here is my code:

var app = new Marionette.Application(); app.addRegions({ modal: '#modal' }); app.vent.on('showModal', function (view) { var modal = app.modal; modal.show(view); modal.$el.modal({ show: true, keyboard: true, backdrop: 'static' }); }); 
+6
source share
3 answers

You can use the event shown as shown in the doc

 modal.$el.on('shown', function () { $('input:text:visible:first', this).focus(); }); 
+11
source

If odupont answer odupont n't help you (like me), try the following:

 $('.modal').on('shown.bs.modal', function () { $('#firstField').focus(); }) 
+5
source

Here is the updated answer:

 $('.modal').on('shown.bs.modal', function () { $('input:first', this).focus(); }); 
0
source

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


All Articles