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

3 Answers 3

11

You can use the shown event as seen in the doc

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

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

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

Here's an updated answer:

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

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.