Click here to Skip to main content
15,902,189 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
AoA,
How can I get the selected text, its begining and ending cursor position, like I did in the following code(which works on textArea)

JavaScript
var tempArea = document.getElementById('MainText').innerHTML;

    var textComponent = document.getElementById(textbox);
    var selectedText;
    var allText = textComponent.value;
    // IE version
    if (document.selection != undefined) {
      //I dont care about IE

    }
        // Mozilla version
    else if (textComponent.selectionStart != undefined) {
        var startPos = textComponent.selectionStart;
        var endPos = textComponent.selectionEnd;
        selectedText = textComponent.value.substring(startPos, endPos);
        if (selectedText.search(arguments[1]) == -1 && selectedText.search(arguments[2]) == -1)
             allText = allText.substring(0, startPos) + arguments[1] + selectedText + arguments[2] + allText.substring(endPos,allText.length);
    }

    document.getElementById(textbox).value = allText;


How can I acheive same thing with editableContent DIV?
Posted

1 solution

 
Share this answer
 
Comments
saad_lah 30-Sep-13 15:40pm    
thanx! :)
saad_lah 30-Sep-13 15:40pm    
http://jsfiddle.net/jwvha/1/
here is another solution!
enhzflep 30-Sep-13 15:44pm    
You're welcome. :)

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