Reputation: 97
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?
Upvotes: 3
Views: 3518
Reputation: 131
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/
Upvotes: 3
Reputation: 16659
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();
});
Upvotes: 5