3

I have a contenteditable div where I need to append div(child) at the caret position?

I successfully added text at caret position using some sample in the stack. But i didn't find a way to append a div at the caret position.

How to do it?

var appendText = "<div class='ui-state-default' contenteditable='false'>
<span>"+ui.draggable.text()+"</span>
<span class='ebAppendFieldClose'>x</span>
</div>"

$("#ebPlaceholder").append(appendText);

Right now i use the normal method (like this).

Ganesh Rengarajan
  • 2,006
  • 13
  • 26
Okky
  • 10,338
  • 15
  • 75
  • 122

1 Answers1

3

not sure what you're trying to do; is it placing the div on the cursor position?

var appendText = "<div class='ui-state-default' contenteditable='false'><span>" + ui.draggable.text() + "</span><span class='ebAppendFieldClose'>x</span></div>"

//I'm using 'click', but it can be whatever you want
$("#ebPlaceholder").on('click', function(e) {
    appendText.css({position: 'absolute',
        top: e.clientY + 'px',
        left: e.clientX + 'px'
    }); 
    e.target.append(appendText);
});
Art
  • 592
  • 3
  • 10