.focus()
, trigger()
, & .blur()
Upon understanding the actual requirement of the original poster (OP) - which was to prevent the button.disabled
element from taking away the focus
from enabled buttons - the updated demonstration successfully achieves this goal. However, it does not apply the same logic to button[disabled]
as those are completely inert. 💀
[disabled]
Attribute Always Functions Correctly
The [disabled]
attribute always functions as expected. On the other hand, the Bootstrap .disabled
class does not. Refer to the tests in the demo below for clarification.
Demo
$('.btn').on('click', function(e) {
console.log('clicked')
});
$('.btn').not('.disabled').on('click', function() {
$('.btn').removeClass('lastFocus');
$(this).addClass('lastFocus');
});
$('.disabled').on('focus', function(e) {
$(this).blur();
$('.lastFocus').trigger('focus');
});
b {
color: cyan
}
u {
color: black;
font-weight: 700
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.2.1/css/bootstrap.min.css" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<button class='btn btn-primary' disabled>
<i class="fa fa-search"></i><b>[disabled] attribute</b>
</button>
<button class='btn btn-primary disabled' disabled>
<i class="fa fa-search"></i><b>[disabled] attribute</b> and <u>.disabled class</u>
</button>
<button class='btn btn-primary disabled'>
<i class="fa fa-search"></i><u>.disabled class</u>
</button>
<button class='btn btn-primary disabled'>
<u>.disabled class</u>
</button>
<button class='btn btn-primary'>
<i class="fa fa-search"></i>enabled
</button>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>