2

I have an Ajax HTML editor and a Dropdown above it. On choosing an item form the Dropdown I want the Text of the selected item in the Dropdown to get pasted at the current cursor position in the AJAX HTML editor. Any ideas..?

3lokh
  • 891
  • 4
  • 17
  • 39
  • See http://stackoverflow.com/questions/1064089/inserting-a-text-where-cursor-is-using-javascript-jquery – jaredhoyt May 09 '12 at 04:14
  • @jaredhoyt hi, thanks for the reply but it's for the TextArea in my case it's Ajax HTML editor. I tried the code by giving the id of the TextArea which get's finally created when rendering the HTML editor.And then it get's stuck at ".focus()" function, an error comes up saying ".focus is not supported as the control is disabled or not acessible" – 3lokh May 09 '12 at 05:32

1 Answers1

3

Yeah at the end of the Third day I finally got a solution for my problem, posting it here so that someone can save there precious time from re-inventing the wheel.

This is my Ajax ATML Editor:

<Ajax:Editor ID="EdtrHTML" runat="server" />

I want the selected text from the dropdown to be pasted at the current cursor position in the HTML editor,so I'm calling the function to insert text(InsertAtCursor) on the "change" event of the dropdown.

As a parameter to the function InsertAtCursor i'm passing the ID of the IFrame which gets created while rendering HTML editor.

 $(document).ready(function () {
            $('#<%:DropDownID.ClientID%>').change(function () {
            var ddltext = $('#<%:DropDownID.ClientID%> option:selected').text();
            var ddltext = ' [' + ddltext + '] '
            InsertAtCursor(idofHTMLEditorIFrame, ddltext);//Function for Insertion
        });
    });

This is the function which inserts Text from the dropdown at the cursor position of Ajax HTML Editor.

 function InsertAtCursor(myField, myValue) {

        if (document.selection) {
            myField.focus();
            sel = document.selection.createRange();
            sel.text = myValue;
        }

        else if (myField.selectionStart == 0 || 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;
        }
    } 

In my case my Ajax Editor was inside an Update panel and so after a partial post back the script stopped working, and i found help here.

Hope this works for you too...Cheers..!!

Community
  • 1
  • 1
3lokh
  • 891
  • 4
  • 17
  • 39
  • Doesn't work for me at all. Can you further explain how it's supposed to work? Also, which browsers was it tested on? – JNF May 22 '13 at 09:07