faqts : Computers : Programming : Languages : JavaScript : Forms : TextAreas/TextFields

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

10 of 16 people (63%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

How can I retain the vertical scroll position of a textarea after changing its contents?

Jan 13th, 2005 16:04
Reha Sterbin, Phil Endecott, http://faqts.com/knowledge_base/view.phtml/aid/7746/fid/130


(Tested in Netscape7.2, IE6, and Firefox1/Mozilla5)

function changeAndSetScroll(box){
    scrollPos = box.scrollTop;
    //
    // make your changes to the textarea here
    //
    box.scrollTop = scrollPos;
}

<textarea name="maintext"></textarea>

<input type="button" value="Change and Set Scroll"
onClick="changeAndSetScroll(document.getElementById('maintext'));">