3

In my current project I have used bootbox to replace my old ordinary alert and confirm boxes to a trendy one.But the problem is after closing or dismissing the bootbox its focus is not returning to my input.

    bootbox.alert("Please create a milestone");
    $('input[name="mile_stone[]"]:first').focus();
    return false;

Everything working as expected except the focus function.Is there any way to solve this problem?

user3107066
  • 97
  • 1
  • 10

2 Answers2

5

See this question: Bind a function to Twitter Bootstrap Modal Close

In the case of a bootbox modal, you can add the following:

$('.bootbox').on('hidden.bs.modal', function() { 
    $('input[name="mile_stone[]"]:first').focus();
});
Community
  • 1
  • 1
mccannf
  • 16,619
  • 3
  • 51
  • 63
3

Create a generic function to focus without repeating the code.

This example is for the bootbox alert, for others (confirm, etc ...) just adapt the code:

<script>
$(function(){
    $('#btnAlerta').on('click',function(){
        falert('modal title','message you want to view','#nome');
    });
});
function falert(tit,msg,obj){
    var box = bootbox.alert({
        size: 'small',
        title: tit,
        message:'<p>'+msg+'</p>'
    });
    box.on('hidden.bs.modal',function(){
        $(obj).focus();
    });
}
</script>

<input type="text" id="nome" name="nome" />
<button type="button" id="btnAlerta">Alert</button>

follows the example in practice https://jsfiddle.net/marsites/tLwo253e/