Skip to content Skip to sidebar Skip to footer

Bootbox.prompt() Set Focus On Text Field

I can't seem to set the focus on Bootbox's textfield automatically. here's my code: function setCode(myCode){ bootbox.confirm('Enter your PIN:

Solution 1:

You can register a shown event handler that sets the focus (as suggested by @Shiny). However, as of Bootstrap 3 and Bootbox v4.x.x, the shown event is namespaced and it is necessary to use the fully qualified name shown.bs.modal to register the event handler. So the code would be:

var box = bootbox.confirm("Enter your PIN: <input id='pin_code' type='password' name='pin_code'></input>", 
    function(result) {
        if(result)
        console.log($("#pin_code").val());
    });

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

Trying to set the focus as you were doesn't work because the dialog isn't visible immediately after the call to bootbox.confirm. Elements that aren't visible can't have focus.


Solution 2:

You can try this code

var box = bootbox.confirm("Enter your PIN: <input id='pin_code' type='password' name='pin_code'></input>", 
        function(result) {
            if(result)
            console.log($("#pin_code").val());
        });
box.on('shown',function(){
    $("#pin_code").focus();
});

Solution 3:

I know that this is old but check that if you're using Chrome, you don't have Dev Tools open.


Post a Comment for "Bootbox.prompt() Set Focus On Text Field"