On my pages after a postback I am trying to return focus to a previously focused element
$(window).load(function() {
$('.focus').focus();
});
$(document).ready(function() {
$('input:not([type=button],[type=submit]), select, textarea').focus(function() {
$(this).addClass('focus');
});
$('input:not([type=button],[type=submit]), select, textarea').blur(function() {
$(this).removeClass('focus');
});
});
My code looks like this and it works even for nested elements but looks a little awkward to me, I am just wondering if there is a better/more optimal way of doing it?
You can chain the selector and put the
focustrigger in the dom ready function. You might even try whitelisting the types ofinputelements you want to focus on instead of blacklist.However, I’m not sure how you are saving which element was focused before the postback in order to output it with the
focusclass on the new page load. If you are doing that somehow, then this code is fine.