Reputation: 1979
I am in very difficult struggle with one effect, which I believe is wanted in Bootstrap, but I want to escape. I have button:
<a data-toggle="modal" data-target="#video" class="btn btn-primary btn-lg" href="#">Video<i class="pe-7s-angle-right pe-2x pe-va" style="line-height: 0.3;"></i></a>
Which opens modal window with some video:
<div class="modal fade video-lightbox in" id="video" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="false" style="display: block;"><div class="modal-backdrop fade in" style="height: 442px;"></div>
So when I close the modal, my button gets in focus automatically. Is this effect wanted in Bootstrap and how can I escape it ? I don`t want my button to be in focus after closing the modal.
Upvotes: 19
Views: 9536
Reputation: 21
By default in Bootstrap link or button gets focus after closing modal. You can escape it with this code:
$('#mymodal').on('hidden.bs.modal', function(event) {
event.relatedTarget.blur();
});
Upvotes: 1
Reputation: 15579
You can use this method, to make the modal-trigger loose focus when you close the modal:
$('#myModal').on('shown.bs.modal', function (e) {
$('#modalTrigger').one('focus', function (e) {
$(this).blur();
});
});
Example:
$(document).ready(function() {
$('#myModal').on('shown.bs.modal', function (e) {
$('#modalTrigger').one('focus', function (e) {
$(this).blur();
});
});
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"></script>
</head>
<body>
<button id="modalTrigger" type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
Open modal
</button>
<!-- The Modal -->
<div class="modal" id="myModal">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">Modal</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal footer -->
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</body>
</html>
Upvotes: 3
Reputation: 668
Blur buttons when modal closes.
$('body').on('hidden.bs.modal', '.modal', function() {
$('.btn').blur();
});
Upvotes: 9
Reputation: 1979
Resolved this by removing
d.trigger("focus")
in bootstrap.min.js:
a.fn.modal = b, a.fn.modal.Constructor = c, a.fn.modal.noConflict = function() {
return a.fn.modal = d, this
}, a(document).on("click.bs.modal.data-api", '[data-toggle="modal"]', function(c) {
var d = a(this),
e = d.attr("href"),
f = a(d.attr("data-target") || e && e.replace(/.*(?=#[^\s]+$)/, "")),
g = f.data("bs.modal") ? "toggle" : a.extend({
remote: !/#/.test(e) && e
}, f.data(), d.data());
d.is("a") && c.preventDefault(), f.one("show.bs.modal", function(a) {
a.isDefaultPrevented() || f.one("hidden.bs.modal", function() {
d.is(":visible") && d.trigger("focus")
})
}), b.call(f, g, this)
})
Upvotes: 0
Reputation: 3516
You can simply write a function into your custom.js file.
$(document).ready(function() {
$('.close')on("click", function() {
$(".btn").blur();
});
});
Upvotes: 2