JS gets the position of the cursor in the input box instance

Source: Internet
Author: User

The Createrange method of selection

Document.selection.createRange () returns the TextRange object based on the current text selection, or returns the Controlrange object based on the control selection.

With execcommand, it is useful in the HTML editor, such as: Text bold, italic, copy, paste, create hyperlinks, and so on.

Example one:

The code is as follows Copy Code

<textarea cols=50 rows=15>
Ha ha. We are all freshmen. We all come to help each other. So that we can progress, we can make money! </textarea>
<input Type=button value= Select Word after clicking I look Onclick=alert (Document.selection.createRange (). Text) >
</form>


Say so much crap let's take a look at the position of the cursor in the input box instance

The code is as follows Copy Code

function Getcursortposition (domobj) {
var position = 0;

if (document.selection) {//for IE
Domobj.focus ();
var sel = Document.selection.createRange ();
Sel.movestart (' character ',-domobj.value.length);

Position = Sel.text.length;
else if (Domobj.selectionstart | | domobj.selectionstart = = ' 0 ') {
Position = Domobj.selectionstart;
}

return position;
}

In JS get the current cursor position in the input box, you can use the following methods, compatible with IE, Chrome, Firefox and Safari

Related reference jquery/40412.htm >jquery get the cursor location   http://www.111cn.net/wy/jquery/40412.htm

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.