I have a div with contentEditable set to true.
What I currently have is a file upload in an iframe which sets the location of the uploaded file as a variable. I then put the caret in the position I want to add it to the div and click another button to add the image.
What I would like to do is put the caret in position and then click the file upload. Then when I have selected the file to upload, it remembers the position the caret was in and adds the image there.
So ultimately I need to store the caret position in a variable.
I think this will make a difference but the div will contain child elements such as a, strong and span tags.
Here's what I have so far for getting the caret position...
function getCaretPosition(editableDiv) {
var caretPos = 0;
sel = window.getSelection();
if (sel.rangeCount) {
range = sel.getRangeAt(0);
if (range.commonAncestorContainer.parentNode == editableDiv) {
caretPos = range.endOffset;
}
}
return caretPos;
}
$('.peCont').bind('keyup mouseup', function() {
$("p").html(getCaretPosition(this));
});
and
<div class="peCont" contenteditable="true">Text goes here along with <b>some <i>HTML</i> tags</b>.</div>
<p></p>