jQuery provides a function focus() which can be called up on an element to pull focus on it.
This function can be used to highlight a button/textbox or any other element and is highly useful when making an application more user friendly, for example, by default focussing the username field when login page loads etc..
But sometimes, even if focus() function has been used then also the element is not able to pull the focus.
For example, one such case can be when we display a pop-up with button on blur event of a textbox and we want to have this button in pop-up to be focussed. Here in such a case, focus() function can be called once pop-up is displayed to put the focus on button. This code will work fine, except for the case when user clicks a link which fires the blur of textbox and pop-up gets displayed.
In such a case, the focus will stay at the anchor tag clicked by the user instead of the pop-up button even if we have used focus() on the button.
This can be an irritating experience for the user who may press enter to select the button from pop-up only to realize later that the anchor tag got executed.
This hack can thus serve the purpose ensuring that the focus is always on the element where we intend it to be.