I have added a javascript function to a custom button in the WebHtmlEditor which adds a logged on users name and the date and time as a time stamp using the insertAtCaret function.
Is there a way to move the caret to the end of the text before doing the insert?
Thanks
Paul
If we have huge amounts of data in the webhtmleditor, why is the cursor not shown at the text end even though we are trying to click at the text end using the mouse? – This especially happens when we are loading with huge HTML text.
Why do we need to use JavaScript for a work around to place the cursor at end? Is there any direct way to achieve this without relying on this work around?
I have built a JavaScript function which is working to some extent. But still I am facing issues with the editor. I have written the detailed steps below to reproduce the error:
1. By default [focus in event] the cursor is shown at text end – successful
2. Clicked somewhere in the middle of the text , cursor displays correctly – successful
3. Then clicking at the text end – Not able to show the cursor at text end
4. Cursor position is not getting refreshed; it is struck at the previous cursor position when clicking at the text end.
Play around by clicking in between the text and try clicking at text end, cursor doesn’t show up at text end. I have created labels while clicking, please check the results.
I have attached the file for your reference, and in CS file I am loading editor with the text I have posted in my previous reply.
Overall Goal: It should behave like a normal textbox supporting html text, ie., user should be able to click anywhere in the editor and able to enter text.
Hi Pavannpt,
My original sample was following:
<script type="text/javascript">function SetCursorToTextEnd(){ var editor = iged_getById('<%=WebHtmlEditor1.ClientID%>'); if (editor._ie) { var range = document.body.createTextRange(); range.moveToElementText(editor._elem); range.moveStart('character', range.text.length); range.collapse(); range.select(); editor.insertAtCaret('<span style="background:red">inserted span</span>'); } }</script><ighedit:WebHtmlEditor ID="WebHtmlEditor1" runat="server"></ighedit:WebHtmlEditor><input type="button" onclick="SetCursorToTextEnd()" value="SetCursorToTextEnd" />
I tested by copying your content with dummy1..dummy22 spans into editor at HTML view. After that I changed to Design view, clicked mouse in the middle of content in order to move caret from the end, clicked button and new text was inserted at the end (as expected).
I also tested scenarion when Text with dummy1..dummy22 was set within Page.OnLoad on server and with processing Focus event (Though similar looks odd to me. Probably there are some special validations in your application).When I set focus to editor by clicking in the middle of content the new object was inserted at the end. So, it worked correctly too. Below are aspx codes which I tested:
<script type="text/javascript">function WebHtmlEditor1_Focus(editor){ if (editor._ie) { var range = document.body.createTextRange(); range.moveToElementText(editor._elem); range.moveStart('character', range.text.length); range.collapse(); range.select(); editor.insertAtCaret('<span style="background:red">inserted span</span>'); } }</script><ighedit:WebHtmlEditor ID="WebHtmlEditor1" runat="server"> <ClientSideEvents Focus="WebHtmlEditor1_Focus" /></ighedit:WebHtmlEditor>
However, I could not get dotted-desing view rectangle for the "dummy19" similar to your attached image. I am not sure what browser will do if you will try to call javascript selection function while browser in that design-edit-mode (wide dotted resizable rectagle). If selection methods fail, then that is the limit of javascript usage.
Yes, this does work if the text inside the editor is plain text, but in my case its combination of normal text [email id, user types in] and htmltext.
I am loading the below content on page load, and calling javascript function onfocus event of the editor. The result is the selection of a span control which is somewhere in the middle, but not at the end. I tried different methods of textrange object[move, moveend, movestart etc., nothing seems to work ] Please check the screenshot.
webHtmlEditor1.Text = "<span id='I_1' title='Select to Delete.' contentEditable='false' unselectable='true' style='color:#00589F'>Dummy 1</span>; <span id='I_2' title='Select to Delete.' contentEditable='false' unselectable='true' style='color:#00589F'>Dummy 2</span>; <span id='I_3' title='Select to Delete.' contentEditable='false' unselectable='true' style='color:#00589F'>Dummy 3</span>; <span id='I_4' title='Select to Delete.' contentEditable='false' unselectable='true' style='color:#00589F'>Dummy 4</span>; <span id='I_5' title='Select to Delete.' contentEditable='false' unselectable='true' style='color:#00589F'>Dummy 5</span>; <span id='I_6' title='Select to Delete.' contentEditable='false' unselectable='true' style='color:#00589F'>Dummy 6</span>; <span id='I_7' title='Select to Delete.' contentEditable='false' unselectable='true' style='color:#00589F'>Dummy 7</span>; <span id='I_8' title='Select to Delete.' contentEditable='false' unselectable='true' style='color:#00589F'>Dummy 8</span>; <span id='I_9' title='Select to Delete.' contentEditable='false' unselectable='true' style='color:#00589F'>Dummy 9</span>; <span id='I_10' title='Select to Delete.' contentEditable='false' unselectable='true' style='color:#00589F'>Dummy 10</span>; <span id='I_11' title='Select to Delete.' contentEditable='false' unselectable='true' style='color:#00589F'>Dummy 11</span>; <span id='I_12' title='Select to Delete.' contentEditable='false' unselectable='true' style='color:#00589F'>Dummy 12</span>; <span id='I_13' title='Select to Delete.' contentEditable='false' unselectable='true' style='color:#00589F'>Dummy 13</span>; <span id='I_14' title='Select to Delete.' contentEditable='false' unselectable='true' style='color:#00589F'>Dummy 14</span>; <span id='I_15' title='Select to Delete.' contentEditable='false' unselectable='true' style='color:#00589F'>Dummy 15</span>; <span id='I_16' title='Select to Delete.' contentEditable='false' unselectable='true' style='color:#00589F'>Dummy 16</span>; <span id='I_17' title='Select to Delete.' contentEditable='false' unselectable='true' style='color:#00589F'>Dummy 17</span>; <span id='I_18' title='Select to Delete.' contentEditable='false' unselectable='true' style='color:#00589F'>Dummy 18</span>; <span id='I_19' title='Select to Delete.' contentEditable='false' unselectable='true' style='color:#00589F'>Dummy 19</span>; <span id='I_20' title='Select to Delete.' contentEditable='false' unselectable='true' style='color:#00589F'>Dummy 20</span>; <span id='I_21' title='Select to Delete.' contentEditable='false' unselectable='true' style='color:#00589F'>Dummy 21</span>; <span id='I_22' title='Select to Delete.' contentEditable='false' unselectable='true' style='color:#00589F'>Dummy 22</span>; " ;
Hi pavannpt,
I suggest you to use body to createTextRange instead of editor._elem (which is DIV). Below is example for you:
function SetCursorToTextEnd(){ var editor = iged_getById('<%=WebHtmlEditor1.ClientID%>'); if (editor._ie) { var range = document.body.createTextRange(); range.moveToElementText(editor._elem); range.moveStart('character', range.text.length); range.collapse(); range.select();// editor.focus(); } }
I have tried using javascript to set cursor position to text end on webhtmleditor focusin event but it alwasy gives me error at the following line [text.createTextRange(); It says undefined], but if i use this with normal textbox it works fine. Please suggest if u have any better solution
function SetCursorToTextEnd() { var editor = iged_getById('<%=panelTo.ClientID%>'); var text = editor._ie ? editor._elem : editor._doc().body; //alert(text.innerText); if (text != null && text.innerText.length > 0) { //if (text.createTextRange) //{ var FieldRange = text.createTextRange(); FieldRange.moveStart('character', text.innerText.length); //FieldRange.moveEnd('character', text.innerText.length); FieldRange.collapse(); FieldRange.select(); //} } }
We are populating SPAN Html controls inside webhtmleditor.
example: webHtmlEditor1.Text = "<span id='I_1' title='Select to Delete.' contentEditable='false' unselectable='true' style='color:#00589F'>Dummy 1</span>;"; etc...