View Full Version : Resolved Problem with smiley insert

10-04-2012, 03:05 AM
Hi, i am having a problem with my smiley insert. It always inserts at the end of the line of text rather than where the cursor is. The smiley needs to be inserted wherever the text cursor is currently located and im not sure how to do that.

Here is the code i have so far. Thanks ;)

function emoticon(id,text) {
var txtarea = document.getElementById(id);
text = ' ' + text + ' ';
if (txtarea.createTextRange && txtarea.caretPos) {
var caretPos = txtarea.caretPos;
caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? caretPos.text + text + ' ' : caretPos.text + text;
} else {
txtarea.value += text;

Philip M
10-04-2012, 08:36 AM
Have you tried asking the author (digioz)?

Note - it is a cursor, not a curser!

10-04-2012, 08:53 AM
Oops my bad, correction made..

No sir i found that function in a file and was not aware that there was an owner. But i will google his name and ask him for sure.

UPDATE Wow released in 2004 and nothing more, dusty and old for sure..

Philip M
10-04-2012, 09:04 AM

Solution to your problem posted at


10-04-2012, 09:30 AM
Thanks appreciate that

10-04-2012, 11:36 PM
Just a quick update for those having this issue. I gave up and left it as it was. In that article on the php academy site, the OP displayed his results and his solution and not even that worked, or at least not for me. It kept taking the whole string of page content smiley and all and replacing it on the page instead of just sticking the smiley in there.

And as far as the other examples, i also could not get them to work. Its probably me im not good with js but i wanted to update here in case so you dont get frustrated when it does not work for you either.

So i put it back the way it was and i figure if they want to put a smiley someplace other than the end of the line, they can copy and paste the smiley code where they want in the text... lol

Peace :)