Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

selecting textfield text 1

Status
Not open for further replies.

pazan

Programmer
Mar 10, 2005
6
CH
Hello,

I need a way to select the text of a textfield but not the whole text. I know the method elem.select() but I need something like elem.select(start, length) where start would be the id of the first character to be selected and length, the length of the selection. I have searched in google and did'nt find any solution...If someone know it...please share it:)
 
hi,

Code:
var myText = new String(document.forms(name_of_form).name_of_textfield.value)

var mySelection = new String(myText.subString(start,end))


e.g: if the text is "Hello there" and put: start=2, end=6
the var mySelection will have "llo th"
 
Try this (The argument elem is a form element):
Code:
function selectRange(elem, start, length){
  var txt = elem.createTextRange();
  txt.moveStart("character", start);
  txt.moveEnd("character", (txt.text.length - length) *-1);
  txt.select();
}

Adam

recursion (n.) See recursion.
 
Thanks a lot adam0101 that's excatly what I needed :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top