Expand selection from current cursor position

4 posts, 0 answers
  1. Martin
    Martin avatar
    2 posts
    Member since:
    Feb 2014

    Posted 02 Feb 2014 Link to this post

    In the RadEditor, if I have  a text selection which spans partial words, how can I programatically expand it that so that the selection includes only full words?

    Example (Square brackets represent the selection).

    Start with this:   THIS IS MY SENTEN[CE IN THE RAD]EDITIOR

    Change to this: THIS IS MY [SENTENCE IN THE RADEDITIOR]
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 03 Feb 2014 in reply to Martin Link to this post

    Hi Martin,

    Please have a look into the sample code snippet which works fine at my end.

    ASPX:
    <telerik:RadEditor ID="RadEditor1" runat="server">
    </telerik:RadEditor>
    <telerik:RadButton ID="RadButton1" runat="server" Text="Expand Selection" AutoPostBack="false"
        OnClientClicked="OnClientClicked1">
    </telerik:RadButton>

    JavaScript:
    <script type="text/javascript">
        function OnClientClicked1(sender, args) {
            var editor = $find("<%=RadEditor1.ClientID %>");
            var s = editor.get_contentArea().textContent;
            var first = s.split("[");
            var second = first[1].split("]");
            var originalword = "";
            firstwords = first[0].split(" ");
            secondwords = second[0].split(" ");
            for (var i = 0; i < (firstwords.length) - 1; i++) {
                originalword += firstwords[i];
                originalword += " ";
            }
            originalword += "[";
            originalword += firstwords[i];
            originalword += secondwords[0];
            for (var i = 1; i < secondwords.length; i++) {
                originalword += " ";
                originalword += secondwords[i];
            }
            originalword += second[second.length - 1];
            originalword += "]";
            editor.get_contentArea().textContent = originalword;
        }
    </script>

    Thanks,
    Princy.
  3. Martin
    Martin avatar
    2 posts
    Member since:
    Feb 2014

    Posted 03 Feb 2014 in reply to Princy Link to this post

    Hi Princy.

    Thanks for your reply, although I don't think I was clear in my question.

    The square brackets in my post were only there to illustrate the start and end points of a user's text selection. They wouldn't actually be present in the content.
  4. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 03 Feb 2014 in reply to Martin Link to this post

    Hi Martin,

    With reference to this forum thread RadEditorSelection Client side API Offers getRange() and SelectRange() methods to get/restore the cursor position. You need to get a reference to the RadEditor Document with editor.get_document(); and create a range. After creating a text range  you will be able to use the methods like move(),moveStart(),moveEnd() and select().

    Thanks,
    Princy.
Back to Top