Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8464127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:38:49+00:00 2026-06-10T14:38:49+00:00

My main problem here has to do with inserting text to the cursor position

  • 0

My main problem here has to do with inserting text to the cursor position of the Ajax control toolkit Html Editor. I have been racking my brains with this for a few days, and I am finally at the last leg of cracking the problem. I’ll set up a little context to better describe my exact issue.

I use the HtmlEditor within an ASP.Net page, and I am attempting to insert values from a DropDownList. I am using client side javascript to fire the selectedindexchanged event. Let’s say for instance that I have some text in the Design Panel like so:

123456789
1234[CaretPosition]56789
123456789

Now, I want to insert the variable ddlText exactly at the point of the caret position.
The way my code is now, it will insert the value to the correct horizontal position, but it seems to ignore the fact that my selection is on the second line. The output will look similar to this:

1234[ddltext]56789
123456789
123456789

Notice how my caret position was on the second line, but the inserted text appears on the first line. Is there any way to get the insert function to insert the text to the correct horizontal AND vertical position?

Here is my script that handles the insert event.

        <script type="text/javascript">

            $(document).ready(function () {
                $('#<%=ddlFields.ClientID%>').change(function () {
                    var ddltext = $('#<%=ddlFields.ClientID%> option:selected').text();
                    insertText(ddltext)
                });
            });

            function insertText(text) {
                var editorField = document.getElementById('MainContent_Editor1_ctl02_ctl00').contentWindow.document.body.innerHTML;
                var range = document.getElementById('MainContent_Editor1_ctl02_ctl00').contentWindow.getSelection().getRangeAt(0);
                var selStart = range.startOffset;
                var selEnd = range.endOffset;
                var innerText = editorField.substring(0, selStart) + text + editorField.substring(selEnd, editorField.length);
                document.getElementById('MainContent_Editor1_ctl02_ctl00').contentWindow.document.body.innerHTML = innerText;                                           
            }
    </script>

I’ve been bashing my head against this one requirement for a bit now, and I could really use a fresh pair of eyes on it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T14:38:51+00:00Added an answer on June 10, 2026 at 2:38 pm

    I was able to get this working properly as per my requirements with a lot of help from random websites around the web. No one site offered clear guidance, but taking what I learned from several, I was able to piece together a function that handled this in Firefox, Chrome, and IE. The ajax control toolkit HTML editor is very buggy on it’s own in Opera when used in an ASP.net page, but that is beyond my ability to control. Here is my javascript that handles the requirement of the question. It is worth noting that in IE, you should change the function that fires the event to trigger on a mousedown instead of a click. Hopefully this function helps aggregate all of the information that is scattered around the web concerning this topic.

        <script type="text/javascript">
    
            $(document).ready(function () {
                var isMSIE = /*@cc_on!@*/false;
                if (isMSIE == false) {
                    $('#<%=btnAddField.ClientID%>').click(function (event) {
                        event.preventDefault();
                        insertText($('#<%=ddlFields.ClientID%> option:selected').val())
                        return false;
                    });
                }
                if (isMSIE == true) {
                    $('#<%=btnAddField.ClientID%>').mousedown(function (event) {
                        event.preventDefault();
                        insertText($('#<%=ddlFields.ClientID%> option:selected').val())
                        return false;
                    });
                }
            });
    
            function insertText(text) {
    
                var sel, range;
                if (window.getSelection) {
                    sel = document.getElementById('YourIFramesClientIDGoesHere').contentWindow.getSelection();
                    var isMSIE = /*@cc_on!@*/false;
    
                    if (sel.rangeCount && isMSIE == false) {
    
                        range = sel.getRangeAt(0);
                        range.deleteContents();
                        textNode = document.createTextNode(text);
                        range.insertNode(textNode);
    
                        range.setStart(textNode, textNode.length);
                        range.setEnd(textNode, textNode.length);
                        sel.removeAllRanges();
                        sel.addRange(range);
                    }
                }
                if (sel.rangeCount && isMSIE == true) {
    
                    range = document.selection.createRange();
                    range.pasteHTML(text);
    
                }
    
            }
        </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can't understand what is a problem here: I have got main.cpp file where I
I have an interesting problem here. I have a MySql database table that has
I have an OO problem here. I have two sessions which has common properties
Here is a small program showing a problem: import Data.Fixed main = do print
The main problem is square-space builds their forms with java-script (YUI). They have are
My main problem is I don't understand how to get the position of the
It may sound odd, but I've been programming games in PHP. The main problem
I have a site that utilizes a bottom fixed position masthead here: http://www.entheospartners.com/newsite/ This
Pretty basic problem here. So I have a Java package that I have created
On saveadullam.org/pages/media.html , the main text section correctly covers the full width in Chrome,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.