I am trying to create an icon in my contenteditable div after clicking on a button with the icon. The issue is that while selecting the icon/button I lose the caret in the text. I want this caret back at its old place after closing the buttons.
I made a JS-Fiddle to illustrate the issue: JSFiddle
PS: The question is especially about the caret. It doesn't matter a lot to me if the icon is inserted or not. That part should be easy to figure out :).
html
<div id="write-mail">
<div style="position: relative;">
<div id="new-mail-content" contenteditable="true">
<div>How to return the caret where it was in the sentence after clicking the buttons?</div>
<div>Maybe prevent the caret from being removed after clicking the buttons?</div>
<div>I know I can store the current selection by using window.getSelection(). If I would be able to reactivate this selection, that would be great!</div>
</div>
<div id="emotepicker">
<button><span>✔</span></button>
<button><span>✯</span></button>
<button><span>✘</span></button>
</div>
</div>
<button class="add-icon">Add Icon</button>
</div>
css
#new-mail-content{
background-color: white;
min-height: 5em;
}
#write-mail button{
height: 30px;
}
#emotepicker{
display: none;
position: absolute;
left: 0px;
right: 0px;
bottom: 0px;
background-color: rgba(0,0,0,0.1);
text-align: center;
}
div{
padding: 2px;
}
jquery/javascript
$(".add-icon").click(function(){
$("#emotepicker").toggle();
});