einstein
einstein

Reputation: 13850

selecting all text causing the browser to scroll to the top

I have this function:

function selectAllTextInInputs(textbox) {
    textbox.focus();
    textbox.select();
}

and when I call this function with a input element as a paremeter. It do what It meant to do, which is to focus and select the text. But there is one flaw. It scrolls to the top of the page. If I have an element far down on my browser window it scrolls back up to the top of the page. How can I fix so the function doesn't scroll?

Upvotes: 0

Views: 644

Answers (2)

Niklas
Niklas

Reputation: 30012

Just drop the focus out from the function. Selecting the content, will automatically give it focus without the scroll.

Example:

http://jsfiddle.net/niklasvh/Y7DRf/1/

Upvotes: 1

Michael Low
Michael Low

Reputation: 24506

The code you've posted shouldn't cause to window to scroll to the top AFAIK. How are you calling the selectAllTextInInputs function ? If it's from a <a> link, try adding return false afterwards to stop it jumping to the top e.g. <a href="#" onClick="selectAllTextInInputs();return false;">

Upvotes: 0

Related Questions