user1736947
user1736947

Reputation: 192

Set textbox active without using focus()

I have small iframe . The content of the iframe is larger than the iframe window and can be seen by scrolling around. There is a button in the iframe. Clicking that button animates for one second and increases height and width of iframe, loads a form and sets a text box as focus.

 $input[0].focus();

However, before the animation starts, the focus() is making the text box the center of the iframe window and distorting what is visible in the iframe window. How can I achieve this : The cursor to be active in that text box but not have the content of the iframe to be re-positioned. The re-positioning happens so that the text-box can be in visible part of iframe.

I cannot put focus() in the callback for animate() due to other constraints.

Upvotes: 1

Views: 172

Answers (1)

user1736947
user1736947

Reputation: 192

As suggested by @Zyga, this worked :

function focusWithoutScrolling (element){
var x = window.scrollX, y = window.scrollY;
element.focus();
window.scrollTo(x,y);

Upvotes: 1

Related Questions