How to add / insert / update text inside contenteditable div at a particular position?
contenteditable, html, javascript, jquery
Solution
Here's how I did it:
var position = 5,
txt = "more ";
var current = $("#txtarea").html();
//alert(current.substring(0, position))
var endVal = current.substring(position);
var newValue = current.substring(0, position) + txt + endVal;
$("#txtarea").html(newValue);
jsfiddle displaying it 'in action'.
Edit: Updated the jsfiddle with the approach listed in a comment above to this post. Pretty slick!
Problem
The HTML code looks like this ``` <div id="txtarea" contenteditable="true">Some text</div> ``` I have to insert some new text based on some event at a particular position in the above div. The event calls the function say updateDiv(txt, positon). For example it says `updateDiv("more ",5);` So the div should become be ``` <div id="txtarea" contenteditable="true">Some more text</div> ``` I tried a lot of javascript and jquery but nothing seem to work.