74569

Editable Div Caret Position

Question:

I have an editable div and I am using a button to insert an image into the div. Right now, I am just doing document.getElementById('elementid').innerHTML. += ; in order to get the image added to the end of the div. I would like to enter the image where the caret is. How would I go about doing this?

Thanks

Answer1:

To insert an element at the caret is not too hard. The following function inserts a node at the caret (or at the end of the selection, if content is selected) in all major browsers:

function insertNodeAfterSelection(node) { var sel, range, html; if (window.getSelection) { sel = window.getSelection(); if (sel.getRangeAt && sel.rangeCount) { range = sel.getRangeAt(0); range.collapse(false); range.insertNode(node); } } else if (document.selection && document.selection.createRange) { range = document.selection.createRange(); range.collapse(false); html = (node.nodeType == 3) ? node.data : node.outerHTML; range.pasteHTML(html); } }

Answer2:

I hate to sound negative, but this is so hard it's ridiculous. You have to deal with IE and others, and the implementations are vastly different. But where it gets uber-hard is that if you click a button to insert the image, you lose focus and the caret position, so you need to remember the position with some onblur bookmarking ability (again, IE different). The focus thing is not so much an issue if your editablecontent is in an iframe and maintains its own focus. (Note: not dissing IE here, I actually prefer their implementation to the W3C standard drek.)

You can look at some open source text editors for clues and hints. But you'll find an enormous amount of code to handle these simple tasks.

Answer3:

Does this help: <a href="http://jsfiddle.net/8akDr/" rel="nofollow">http://jsfiddle.net/8akDr/</a>

Recommend

  • How to show timestamp x-axis in Python Plotly
  • Coalescing results in Linq
  • Accessing AngularJs Controller properties from the root node ng-if
  • Missing “Annotation processing” menu in eclipse
  • Compare two table and find matching columns
  • Bootstrap close modal not working
  • find command search only non hidden directories
  • CursorWindowAllocationException in standard ORMLite method
  • Trace: The node type SpreadProperty has been renamed to SpreadElement at Object.isSpreadProperty
  • Not able to boot from usb
  • PHP deleting from database not working
  • Can we use AmqpItemReader and AmqpItermWriter for request/reply use case in spring batch?
  • Referencing a table in web2py before defining it
  • Does OAuth “state” mitigate any genuinely dangerous attacks?
  • create multiple text files from a list using batch file
  • isotope shakes after the transition
  • EditText doesn't start automatically
  • java outlook send mail
  • Need to pass object and operation in a function that executes it
  • Why is this Animatable property being set again?
  • jsx command not found on mac terminal
  • SQL Worksheet is not displaying in SQL Developer
  • Hibernate Idempotent Update
  • No Gradle file syntax highlighting in Eclipse Mars.2
  • Can't hide status bar in AVPlayerViewController's portrait mode
  • What are advantages/disadvantages of using Selenium for Java vs .NET applications?
  • JavaMail connection problems [duplicate]
  • Delphi Prism getting Unknown Identifier “DllImport” error
  • how to specify different css for ie
  • Content-Type alternative in MQTT
  • JavaScript Regex to Match Boundaries of Words with diacritics
  • How to turn off notice reporting in xampp?
  • how to run ejabberd with Erlang on Heroku?
  • How to handle div that is created dynamically in a table
  • Call Microservice from another Microservice within Docker
  • Firebase: How to read from external DB?