6

I am using Bootstrap, from Twitter for genereting modal-dialog in a Marionette Application.

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

Here 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'
    });
});
js999
  • 2,063
  • 4
  • 26
  • 34
  • Possible duplicate of [Twitter bootstrap modal input field focus](http://stackoverflow.com/questions/15474862/twitter-bootstrap-modal-input-field-focus) – Muhammad Rehan Saeed Oct 13 '15 at 13:46

3 Answers3

11

You can use the shown event as seen in the doc

modal.$el.on('shown', function () {
  $('input:text:visible:first', this).focus();
});
odupont
  • 1,946
  • 13
  • 16
5

If odupont's answer didn't help you,(like me,) try this:

$('.modal').on('shown.bs.modal', function () {
    $('#firstField').focus();
})
ParPar
  • 7,355
  • 7
  • 43
  • 56
0

Here's an updated answer:

$('.modal').on('shown.bs.modal', function () {
    $('input:first', this).focus();
});
Expenzor
  • 432
  • 1
  • 5
  • 17