Skip to content

Infragistics Community Forum / Web / Ignite UI for jQuery / HTMLEditor insert text at current cursor position

HTMLEditor insert text at current cursor position

New Discussion
Steve Hind
Steve Hind asked on Nov 28, 2013 2:47 PM

Hi, I’m using the jQuery ignite UI HTML Editor (v 2013.1)

I’m wishing to insert some text at the current cursor position.

The only references in the API I can find relates to invoking “getContent”/”setContent” like:

var currentContent = $(“#editor”).igHtmlEditor(“getContent”, “html”);

$(“#editor”).igHtmlEditor(“setContent”, currentContent + “{My Text}”, “html”);

 

But, this will only allow me to insert at the beginning or append the text, how can I insert text at the current cursor position?

Thanks

Steve

Sign In to post a reply

Replies

  • 0
    [Infragistics] Petar Ivanov
    [Infragistics] Petar Ivanov answered on Nov 25, 2013 11:27 AM

    Hello Steve,

    I am attaching a sample illustrating how the caret position in igHtmlEditor may be tracked in order to be able to insert content specifically at the caret position:

    <script>
        var focusElement;   //keep the element at which the caret was lastly
        var selectionRange  //for IE7&8
     
     
        $(function () {
     
            $("#htmlEditor").igHtmlEditor();
     
            //Handle key and mouse up for the editor's input area in order to keep track of the caret position
            $($('#htmlEditor').find('iframe').contents()[0]).bind('mouseup keyup'function () {
                if (this.getSelection !== undefined) {
                    focusElement = this.getSelection().focusNode;
                } else {
                    selectionRange = this.selection.createRange();
                }
            });
     
            $("#button").click(function() {
                var addedText= "THIS IS SOME INSERTED TEXT";
     
                if (focusElement !== undefined) {
                    $(focusElement).after(addedText);
                } else if (selectionRange !== undefined) {  //IE7 and IE8
                    selectionRange.pasteHTML(addedText);
                } else {
                    var currentContent = $("#htmlEditor").igHtmlEditor("getContent""html");
                    $("#htmlEditor").igHtmlEditor("setContent", currentContent + addedText, "html");
                }  
            })
     
        });
    </script>

    Note that IE7&8 require separate caretPosition tracking via selectionRange.

    Hope this helps. Please do not hesitate to contact me if you have any questions.

    • 0
      [Infragistics] Petar Ivanov
      [Infragistics] Petar Ivanov answered on Nov 28, 2013 2:47 PM

      Hello Steve,

      Please do not hesitate to contact me if you have any questions regarding this scenario.

  • You must be logged in to reply to this topic.
Discussion created by
Favorites
Replies
Created On
Last Post
Discussion created by
Steve Hind
Favorites
0
Replies
2
Created On
Nov 28, 2013
Last Post
12 years, 3 months ago

Suggested Discussions

Created by

Created on

Nov 28, 2013 2:47 PM

Last activity on

Feb 23, 2026 7:11 AM