This article describes the way JS gets and sets the textarea or input text box to select the text location. Share to everyone for your reference. The implementation methods are as follows:
function GetPos (EL) {
var range, TextRange, duplicate
el.focus ()
if (El.selectionstart) return El.selectionstart
Else if (document.selection) {//IE
range = Document.selection.createRange ()
if (range = = null) return el.value.length
TextRange = El.createtextrange ()
duplicate = Textrange.duplicate ()
Textrange.movetobookmark (Range.getbookmark ())
duplicate.setendpoint (' Endtostart ', TextRange)
return Duplicate.text.length
}
}
function SetPos (el, POS) {
var range
El.focus ()
if ( El.setselectionrange)
El.setselectionrange (POS, POS)
else if (el.createtextrange) {
range.collapse ( true)
range.moveend (' character ', POS)
range.movestart (' character ', POS)
range.select ()
}
}
I hope this article will help you with your JavaScript programming.