I have a button
<button id="emoji"><img src="emoji.png"></button>
I have the following Javascript code
$(function () {
$('#emoji1').on('click', function() {
var img = document.createElement('img');
img.className = 'emoji';
img.src = 'stick/kiss.png';
var text = $('#text');
text.val(text.val() + img);
});
})();
I want that when pressing the button it adds the image in a <textarea id="text">
and that I can continue typing.
What you are trying to do is not possible. A type tag
<textarea />
is designed to contain text only. What you can do is set this as the background image, as well as make the textarea transparent viabackground-color: transparent
CSS.You can't do it with
textarea
directly becausetextarea
by definition it only supports text. What you could do is create your own rich text editor (RTF), although I would sincerely recommend you to go for other already extended options (such as TinyMCE) because it is an arduous and complex task.This solution would require one
iframe
or onediv
that is editable, create your own buttons that will perform different functions and, when the necessary action is to be performed (send the form, or the editable field loses focus), update the value of the textarea ("invisible") specifically.Here is a basic example (click on "Submit form" to see the value of the textarea):
But as you can see it is very basic (the emoticon is only placed at the end and not where the cursor is), if you really want to create a more complete RTF that would require much more: calculating the position of the cursor to place the emoticon there, different functionalities ( bold, italic, etc) and it doesn't seem like it, but it starts to add up and add up and can end up being unbelievably big.
I'm new to this, I'll add the code below and good luck mate.
What this code does is that when you press an emoticon or an image in this case "an image like the WhatsApp faces that I use" and what it does is add the same face in a
div
and continue writing next to the inserted face .This is the HTML code:
This is the part of the SCRIPT:
What this code does is that when you press an emoticon or an image in this case "an image like the WhatsApp faces that I use" and what it does is add the same face in a div and continue writing next to the face inserted
With textarea it may not be possible (icons could be used if they are included in the characters of a font), but through an editable div it is possible to add custom icons.
Example by andrewfiorillo