Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 644093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:21:13+00:00 2026-05-13T21:21:13+00:00

On my pages after a postback I am trying to return focus to a

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T21:21:14+00:00Added an answer on May 13, 2026 at 9:21 pm

    You can chain the selector and put the focus trigger in the dom ready function. You might even try whitelisting the types of input elements you want to focus on instead of blacklist.

    $(document).ready(function(){
        $('.focus').focus();
        $('input[type=text], select, textarea').focus(function() {
            $(this).addClass('focus');
            // To save the focused value to a hidden field
            $('#id-of-Hidden-Field').val($(this).attr('name'));
    
        }).blur(function() {
            $(this).removeClass('focus');
            // remove the focus hidden field
            $('#id-of-Hidden-Field').val('');
        }); 
    });
    

    However, I’m not sure how you are saving which element was focused before the postback in order to output it with the focus class on the new page load. If you are doing that somehow, then this code is fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to autoreload my page after every 20 seconds. I am using
After trying to understand why client code is not rendered in a page (injected
Shell (explorer.exe www.google.com) is how I'm currently opening my products ad page after successful
I want to send an Email message after a page is returned to the
After seeing the cool new reputation tab on the stackoverflow user page, I was
I have a page where I would like it to remain static after refresh
Is it allowable to pass parameters to a web page through the URL (after
I have this .NET web app that draws a table in Page_Load. Then after
WPF, Browserlike app. I got one page containing a ListView. After calling a PageFunction
Large pages are available in Windows Server 2003 and Windows Vista . But how

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.