Returns text and htmltext of createTextRange.
<Script language = "JavaScript">
Function Test ()
{
VaR RNG = Document. Body. createTextRange ();
Alert (RNG. Text)
}
Function test1 ()
{
VaR RNG = Document. Body. createTextRange ();
Alert(rng.html text)
}
</SCRIPT>
<Input type = "button" onclick = "test ()" value = "text">
<Input type = "button" onclick = "test1 ()" value = "htmltext">
Obtain the selected text in the specified text box: only the first text box is returned.
<Input id = "inp1" type = "text" value = "1234567890">
<Input id = "inp2" type = "text" value = "9876543210">
<Input type = "button" onclick = "test ()" value = "OK">
<Script language = "JavaScript">
Function Test ()
{
VaR o = Document. getelementbyid ("inp1 ")
VaR r = Document. selection. createRange ();
If (O. createTextRange (). inrange (r ))
Alert (R. Text );
}
</SCRIPT>
Reverse search of page text
Ababababababa
<Input value = "reverse query a" onclick = myfindtext ("A") type = "button">
<Script language = 'javascript '>
VaR RNG = Document. Body. createTextRange ();
Function myfindtext (text)
{
RNG. Collapse (false );
If (RNG. findtext (text,-1, 1 ))
{
RNG. Select ();
RNG. Collapse (true );
} Else
{Alert ("end ");}
}
</SCRIPT>
Focus the control and place the cursor at the end.
<Script language = "JavaScript">
Function setfocus ()
{
VaR OBJ = event. srcelement;
VaR TXT = obj. createTextRange ();
TXT. movestart ('character ', obj. value. Length );
TXT. Collapse (true );
TXT. Select ();
}
</SCRIPT>
<Input type = "text" value = "http://toto369.net" onfocus = "setfocus ()">
Obtain the cursor position in the text box.
<Script language = "JavaScript">
Function getpos (OBJ ){
OBJ. Focus ();
VaR S = Document. selection. createRange ();
S. setendpoint ("starttostart", obj. createTextRange ())
Alert (S. Text. Length );
}
</SCRIPT>
<Input type = "text" id = "txt1" value = "1234567890">
<Input type = "button" value = "Get cursor position" onclick = getpos (txt1)>
Control the cursor position in the input box
<Script language = "JavaScript">
Function setpos (Num)
{
Text1.focus ();
VaR E = Document. getelementbyid ("text1 ");
VaR r = E. createTextRange ();
R. movestart ('character ', num );
R. Collapse (true );
R. Select ();
}
</SCRIPT>
<Input type = "text" id = "text1." value = "1234567890">
<Select onchange = "setpos (this. selectedindex)">
<Option value = "0"> 0 </option>
<Option value = "1"> 1 </option>
<Option value = "2"> 2 </option>
<Option value = "3"> 3 </option>
<Option value = "4"> 4 </option>
<Option value = "5"> 5 </option>
<Option value = "6"> 6 </option>
<Option value = "7"> 7 </option>
</SELECT>
Select a text section in the text box
<Script language = JavaScript>
Function sel (OBJ, num)
{
VaR RNG = obj. createTextRange ()
VaR sel = RNG. Duplicate ();
Sel. movestart ("character", num );
Sel. setendpoint ("endtostart", RNG );
Sel. Select ();
}
</SCRIPT>
<Input type = "text" id = "text1." value = "1234567890">
<Select onchange = "sel (text1, this. Value)">
<Option value = "0"> 0 </option>
<Option value = "1"> 1 </option>
<Option value = "2"> 2 </option>
<Option value = "3"> 3 </option>
<Option value = "4"> 4 </option>
<Option value = "5"> 5 </option>
<Option value = "6"> 6 </option>
<Option value = "7"> 7 </option>
</SELECT>
Control the movement of the cursor in the text box
<Input type = "button" value = "<" onclick = go (-1)>
<Input id = "Demo" value = "here is the text">
<Input type = "button" value = ">" onclick = go (1)>
<Script language = "JavaScript">
Function go (n ){
Demo. Focus ();
With (document. selection. createRange ())
{
Movestart ("character", N );
Collapse ();
Select ();
}
}
</SCRIPT>