2

I am making a project where users can click on the smileys and they get inserted in contenteditable div.

  1. I want three divs and in whatever div I am, the smiley should insert in that div.
  2. Also, here the problem is that smileys only insert at the end of the div. I want that smiley should enter only wherever is the cursor.

Note: please check that the size of smileys should remain same in all the divs.

<div id="text_wrapper">
    <div id="text" contentEditable="true" hidefocus="true"></div>
</div>
<div id="text_wrapper">
   <div id="text1" contentEditable="true" hidefocus="true"></div>
</div>
<div id="text_wrapper">
   <div id="text2" contentEditable="true" hidefocus="true"></div>
</div>
<span id="button">Add Emoji</span>

$("#button").click(function() {
  $("#text").append('<img src="https://cdn.okccdn.com/media/img/emojis/apple/1F60C.png"/>');
});


Demo

István Pálinkás
  • 2,217
  • 7
  • 25
  • 50
Amanjot Kaur
  • 2,028
  • 4
  • 18
  • 33
  • Your question is not clear.. Kindly elaborate. – Ajay Suwalka Jul 20 '15 at 10:05
  • To get the cursor position within a contentEditable element you can take a look at this question: http://stackoverflow.com/questions/4767848/get-caret-cursor-position-in-contenteditable-area-containing-html-content – maja Jul 20 '15 at 10:06
  • 1
    Use focusin event to set the current div (See https://jsfiddle.net/x6ud5wxt/3/). Also see http://stackoverflow.com/questions/1181700/set-cursor-position-on-contenteditable-div for how to control the cursor position after adding the smiley.. – Lars Juel Jensen Jul 20 '15 at 10:13

1 Answers1

4

First: insert into the right one of the three elements:

You are using the expression #text which refers to the first editable div.

If You'd like to target the one with the last focus on it, You can use classes for this.

Add a class to them, so You can easily target any of them

<div id="text_wrapper">

    <div id="text" class="editable" contentEditable="true" hidefocus="true"></div>

</div>

<div id="text_wrapper">

   <div id="text1" class="editable" contentEditable="true" hidefocus="true"></div>

</div>

<div id="text_wrapper">

   <div id="text2" class="editable" contentEditable="true" hidefocus="true"></div>

</div>

<span id="button">Add Emoji</span>

Then You can easily decide where the focus is with a single event listener

$( document ).on( "click focusin" , ".editable" , function(){

    $( ".editable" ).removeClass( "focus" );
    $( this ).addClass( "focus" );

} );

From this point, the focused item ( the one with the cursor ) will have the class ".focus".

Now, You can use

$( document ).on( "click" , "#button" , function() {

  $( ".editable.focus" ).append( '<img src="https://cdn.okccdn.com/media/img/emojis/apple/1F60C.png"/>' );

});




Second: insert at the position of the cursor

This seems to be a bit more complex, but there are pretty clean ways. Take a look at this topic.

Community
  • 1
  • 1
István Pálinkás
  • 2,217
  • 7
  • 25
  • 50
  • 1
    I have just made the fiddle with your solution.. https://jsfiddle.net/x6ud5wxt/4/ – Saiyan Prince Jul 20 '15 at 10:16
  • Thanks @steven. But I am not getting how to get the cursor position. Also, if I had multiple smileys , how will i manage them. could you plz give me an idea – Amanjot Kaur Jul 20 '15 at 12:30
  • I tried your code. I want to know when I insert smiley in the first div, the smiley is small, but in the other divs, it gets bigger. @steven palinkas – Amanjot Kaur Jul 20 '15 at 12:44
  • Your declaration in the CSS: `#text img { width: 50px; }` can be corrected to target all the editable divs `.editable img { width: 50px; }` – István Pálinkás Jul 20 '15 at 13:45