Focus the first field in modal forms
Asked Answered
G

3

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'
    });
});
Grochow answered 26/7, 2012 at 11:16 Comment(1)
Possible duplicate of Twitter bootstrap modal input field focusWitchery
C
11

You can use the shown event as seen in the doc

modal.$el.on('shown', function () {
  $('input:text:visible:first', this).focus();
});
Cannoneer answered 26/7, 2012 at 11:49 Comment(0)
I
5

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

$('.modal').on('shown.bs.modal', function () {
    $('#firstField').focus();
})
Indian answered 4/3, 2014 at 13:30 Comment(0)
P
0

Here's an updated answer:

$('.modal').on('shown.bs.modal', function () {
    $('input:first', this).focus();
});
Punjab answered 25/11, 2015 at 16:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.