Click here to Skip to main content
15,891,777 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
AoA,
I have found the following code from somewhere...I have changed the mozilla version and its working fine... but i am unable to get the start and end position of selected text from createrange() method


//JavaScript.js

JavaScript
function ShowSelection() {
    var textComponent = document.getElementById('MainTextArea');
    var selectedText;
    var allText = document.getElementById('MainTextArea').value;
    // IE version
    if (document.selection != undefined) {
        textComponent.focus();
        var sel = document.selection.createRange();
   
        selectedText = sel.text;

        var slen = sel.moveStart('character', -oField.value.length);
        var elen = sel.text.length;
        allText.value = allText.value.substring(0, slen) + "" + selectedText;

    }
        // Mozilla version
    else if (textComponent.selectionStart != undefined) {
        var startPos = textComponent.selectionStart;
        var endPos = textComponent.selectionEnd;
        selectedText = textComponent.value.substring(startPos, endPos);

        allText = allText.substring(0, startPos) + "" + selectedText + "" + allText.substring(endPos,allText.length);
    }

    document.getElementById('MainTextArea').value = allText;
}



although mozilla version is running fine..
is there any way to get start and end position from createrange() method?
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900