js999
js999

Reputation: 2083

Focus the first field in modal forms

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'
    });
});

Upvotes: 6

Views: 5571

Answers (3)

Expenzor
Expenzor

Reputation: 461

Here's an updated answer:

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

Upvotes: 0

ParPar
ParPar

Reputation: 7569

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

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

Upvotes: 5

odupont
odupont

Reputation: 1976

You can use the shown event as seen in the doc

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

Upvotes: 11

Related Questions