Use createTextRange in JavaScript to obtain the cursor position of input and textarea.

Source: Internet
Author: User

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.htmlText)
}
</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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.