nico55555
nico55555

Reputation: 65

Prevent scrolling to input box when Javascript Focus is used

I would like the cursor to be automatically placed in a textbox when my page loads. However, I do not want the page to scroll down to this textbox (I would prefer that it remains out of view at the bottom of the page). This probably sounds odd, but I do have a need for it!

I make the cursor appear using this code:

<script>document.getElementById('textbox1').focus()</script>

Is anyone able to modify this code such that scrolling will not occur?

Upvotes: 2

Views: 2262

Answers (1)

david
david

Reputation: 4278

The window object supports a scrollTo() method try adding it after focus()

document.getElementById('textbox1').focus(); window.scrollTo(0,0);

Upvotes: 1

Related Questions