Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

get caret position javascript

function getCaretCharacterOffsetWithin(element) {
    var caretOffset = 0;
    var doc = element.ownerDocument || element.document;
    var win = doc.defaultView || doc.parentWindow;
    var sel;
    if (typeof win.getSelection != "undefined") {
        sel = win.getSelection();
        if (sel.rangeCount > 0) {
            var range = win.getSelection().getRangeAt(0);
            var preCaretRange = range.cloneRange();
            preCaretRange.selectNodeContents(element);
            preCaretRange.setEnd(range.endContainer, range.endOffset);
            caretOffset = preCaretRange.toString().length;
        }
    } else if ( (sel = doc.selection) && sel.type != "Control") {
        var textRange = sel.createRange();
        var preCaretTextRange = doc.body.createTextRange();
        preCaretTextRange.moveToElementText(element);
        preCaretTextRange.setEndPoint("EndToEnd", textRange);
        caretOffset = preCaretTextRange.text.length;
    }
    return caretOffset;
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: jquery word count 
Javascript :: bootstrap multiselect change value 
Javascript :: jqurey cdn 
Javascript :: moment to date 
Javascript :: set data attribute with a string jquery 
Javascript :: javascript padend 
Javascript :: react native scrollview full height 
Javascript :: get element by class 
Javascript :: electron getPath 
Javascript :: JAVASCRIPT ARRRAY LOOP BACKWARDS 
Javascript :: how to detect account change in metamask 
Javascript :: toggle bollean value in js 
Javascript :: js wait command 
Javascript :: js get element type 
Javascript :: compare NaN in javascript if condititon 
Javascript :: get element or class 
Javascript :: how to set/get cookie in JavaScript 
Javascript :: random image and link js 
Javascript :: how to test usestate in jest 
Javascript :: how to set a faviconin htm;l 
Javascript :: how to call datetime in javascript 
Javascript :: Error: `createStackNavigator()` has been moved to `react-navigation-stack`. 
Javascript :: Parcel, how to fix the `regeneratorRuntime is not defined` error 
Javascript :: jquery is check 
Javascript :: object delete with id filter javascript 
Javascript :: react native run on device command line 
Javascript :: Round off a number to the next multiple of 5 using JavaScript 
Javascript :: jest testmatch specific folder 
Javascript :: lottie react 
Javascript :: redux saga delay 
ADD CONTENT
Topic
Content
Source link
Name
5+3 =