frenzy
frenzy

Reputation: 335

jQuery: return focus after loading dialog

is there a simple way to remember the element which had focus on before the loading dialog is shown, and return the focus to that element? There is a search button on the page which sends an ajax request on keyboard press to fetch the data from the server. On each ajax request the modal loading dialog shows (notice that it is jquery dialog), and after the response, the focus on the search element is lost, and I want it back.

Thanks.

Upvotes: 3

Views: 2871

Answers (1)

jfriend00
jfriend00

Reputation: 707526

Before you put the dialog up, you can save the location of the focus with this:

var origFocus = document.activeElement;

And, then after the dialog goes away, you can restore it:

origFocus.focus();

Upvotes: 7

Related Questions