[CLOSED] Add Text to the current cursor-position in a textarea or htmleditor

  1. #1

    [CLOSED] Add Text to the current cursor-position in a textarea or htmleditor

    Hello,

    is there a way to add text to the current cursor-position of a textarea or htmleditor or whatever...?

    E.g. i have the following code:

    <ext:ToolbarButton ID="AddLinkButton" runat="server" StandOut="true" Icon="LinkAdd"
        Text="Link hinzufügen">
        <Listeners>
            <Click Handler="#{HtmlEditor}.setValue(#{HtmlEditor}.getValue()+addLink(#{PageUid}.getValue(),#{DataId}.getValue(), #{DataId}.getText(), #{LinkName}.getValue()))" />
        </Listeners>
    </ext:ToolbarButton>
    My AddLinkButton adds a special linl to my HtmlEditor. But -of course- the link is always added at the end of the HtmlEditor-TextArea.
    Is there a way to get the current (or last) cursor-position of the HtmlEditor and add the link or any other string there?

    Regards,

    Martin

  2. #2

    RE: [CLOSED] Add Text to the current cursor-position in a textarea or htmleditor

    Hi,

    Please see the following sample
    <%@ Page Language="C#" %>
    
    <%@ Register Assembly="Coolite.Ext.Web" Namespace="Coolite.Ext.Web" TagPrefix="ext" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <title></title>
        <script type="text/javascript">
            function insertAtCursor(myField, myValue) {
                  myField = myField.el.dom;
                  if (&#100;ocument.selection) {
                          myField.focus();
                          sel = &#100;ocument.selection.createRange();
                          sel.text = myValue;
                  } 
                  else if (myField.selectionStart || myField.selectionStart == '0') {
                      var startPos = myField.selectionStart;
                      var endPos = myField.selectionEnd;
                      myField.value = myField.value.substring(0, startPos)
                                  + myValue
                                  + myField.value.substring(endPos, myField.value.length);
    
                  } else {
                    myField.value += myValue;
                  }
            }
    
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <ext:ScriptManager ID="ScriptManager1" runat="server">
            </ext:ScriptManager>
            
            <ext:HtmlEditor ID="Editor1" runat="server">        
            </ext:HtmlEditor>
            <ext:Button runat="server" Text="Paste">
                <Listeners>
                    <Click Handler="Editor1.insertAtCursor('&amp;nbsp;Some&amp;nbsp;text&amp;nbsp;');" />
                </Listeners>
            </ext:Button>
            
            <ext:TextArea ID="Editor2" runat="server" Width="300" Height="100"></ext:TextArea>
            <ext:Button runat="server" Text="Paste">
                <Listeners>
                    <Click Handler="insertAtCursor(Editor2, ' Some text ');" />
                </Listeners>
            </ext:Button>
            
            <ext:TextField ID="Editor3" runat="server" Width="300"></ext:TextField>
            <ext:Button runat="server" Text="Paste">
                <Listeners>
                    <Click Handler="insertAtCursor(Editor3, ' Some text ');" />
                </Listeners>
            </ext:Button>
        </form>
    </body>
    </html>
    Vladimir Shcheglov
    Sr. Developer
  3. #3

    RE: [CLOSED] Add Text to the current cursor-position in a textarea or htmleditor

    Thank you very much.

    It works fine!
  4. #4

    RE: [CLOSED] Add Text to the current cursor-position in a textarea or htmleditor

    hi,
    We are using just HTMLEditor and no specific textArea.we want insert the text at cursor position.
    we are using following code.

        Editor1.insertAtCursor("some text");
    we have tried with above code,but doesn't work in IE .It works well in all other browsers.in IE it inserts at top
    position.
    We also tried your code but it also doesn't work for us.

    please help to resolve this issue in IE.
    Thanks,
    YobNet Team.

  5. #5

    RE: [CLOSED] Add Text to the current cursor-position in a textarea or htmleditor

    Vladimir Shcheglov
    Sr. Developer
  6. #6
    Here is the example for Ext.NET v1.x.

    Example Ext.NET v1.x
    <%@ Page Language="C#" %>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Ext.NET Example</title>
        <script type="text/javascript">
            var insertAtCursor = function (myField, myValue) {
                myField = myField.el.dom;
                if (document.selection) {
                    myField.focus();
                    sel = document.selection.createRange();
                    sel.text = myValue;
                } else if (myField.selectionStart || myField.selectionStart == '0') {
                    var startPos = myField.selectionStart;
                    var endPos = myField.selectionEnd;
                    myField.value = myField.value.substring(0, startPos)
                                  + myValue
                                  + myField.value.substring(endPos, myField.value.length);
    
                } else {
                    myField.value += myValue;
                }
            };
        </script>
    </head>
    <body>
        <ext:ResourceManager runat="server" />
        <ext:HtmlEditor ID="HtmlEditor1" runat="server" />
        <ext:Button runat="server" Text="Paste">
            <Listeners>
                <Click Handler="HtmlEditor1.insertAtCursor('&amp;nbsp;Some&amp;nbsp;text&amp;nbsp;');" />
            </Listeners>
        </ext:Button>
        <ext:TextArea ID="TextArea1" runat="server" Width="300" Height="100" />
        <ext:Button runat="server" Text="Paste">
            <Listeners>
                <Click Handler="insertAtCursor(TextArea1, ' Some text ');" />
            </Listeners>
        </ext:Button>
        <ext:TextField ID="TextField1" runat="server" Width="300" />
        <ext:Button runat="server" Text="Paste">
            <Listeners>
                <Click Handler="insertAtCursor(TextField1, ' Some text ');" />
            </Listeners>
        </ext:Button>
    </body>
    </html>

Similar Threads

  1. Replies: 2
    Last Post: Mar 19, 2012, 12:08 PM
  2. Replies: 11
    Last Post: Feb 29, 2012, 4:57 PM
  3. [CLOSED] Cursor position
    By Pablo_Azevedo in forum 1.x Premium Help
    Replies: 1
    Last Post: Mar 23, 2011, 5:55 PM
  4. [CLOSED] background image position in HTMLeditor
    By speedstepmem3 in forum 1.x Premium Help
    Replies: 15
    Last Post: May 10, 2010, 3:06 AM
  5. [CLOSED] Messagebox prompt set cursor position to end
    By jchau in forum 1.x Premium Help
    Replies: 5
    Last Post: Jan 20, 2010, 12:40 AM

Tags for this Thread

Posting Permissions