PDA

View Full Version : JS Insert smileys



NeoDreamer
June 7th, 2008, 10:45 PM
http://www.only-jojo.com/temp/english/news/comments.php?id=15

When the user clicks a smiley icon, javascript should insert some smiley text into the message box. For some reason, my insertion function only works when the user has not typed any normal text into the message box. It won't insert any smiley text after the user edits their message.



<script type="text/javascript">
function insertSmiley(smiley)
{
var currentText = document.getElementById("message");
var smileyWithPadding = " " + smiley + " ";
currentText.innerHTML += smileyWithPadding;
}
</script>




<textarea name="message" rows="5" id="message"></textarea>

<img src="interface/bigSmile.gif" onclick="insertSmiley(':-D')" />
<img src="interface/cantLook.gif" onclick="insertSmiley(':-P')" />
<img src="interface/cry.gif" onclick="insertSmiley(':=(')" />
<img src="interface/devious.gif" onclick="insertSmiley('=:)')" />
<img src="interface/huh.gif" onclick="insertSmiley('o_0')" />
<img src="interface/knockedOut.gif" onclick="insertSmiley('x-)')" />
<img src="interface/puzzled.gif" onclick="insertSmiley(':-~')" />
<img src="interface/regularSmile.gif" onclick="insertSmiley(':-)')" />
<img src="interface/shocked.gif" onclick="insertSmiley(':-o')" />
<img src="interface/snooze.gif" onclick="insertSmiley('z:-)')" />
<img src="interface/tongue.gif" onclick="insertSmiley(':-P')" />
<img src="interface/unsure.gif" onclick="insertSmiley(':-/')" />
<img src="interface/upset.gif" onclick="insertSmiley(':-(')" />

Raydred
June 7th, 2008, 11:14 PM
this line:


currentText.innerHTML += smileyWithPadding;

should be:

currentText.value += smileyWithPadding;

that will fix it =)

NeoDreamer
June 7th, 2008, 11:35 PM
Thanks. That did the trick.

What is the difference between "value" and "innerHTML" ?

And how do you put the blinking cursor at the end of the text after someone inserts a smiley? I noticed that the user must click again to resume typing.

Raydred
June 8th, 2008, 04:31 PM
Thanks. That did the trick.

What is the difference between "value" and "innerHTML" ?

And how do you put the blinking cursor at the end of the text after someone inserts a smiley? I noticed that the user must click again to resume typing.

The .Value are used on input elements , such as Text boxes, text areas etc, where innerHTML are used for divs (and maybe spans, i forget).

THe .innerHTML was actually saving the smileys , but it wasn't displaying.

if you want the cursor back in the text area, you need to focus on that control:

currentText.focus();

So the complete code would be:

<script type="text/javascript">
function insertSmiley(smiley)
{

var currentText = document.getElementById("message");

var smileyWithPadding = " " + smiley + " ";
currentText.value += smileyWithPadding;
currentText.focus();

}
</script>

hariku
August 12th, 2008, 05:33 AM
How to display this in guestbook??? as image... plz help me...

NeoDreamer
August 12th, 2008, 02:52 PM
How to display this in guestbook??? as image... plz help me...

I will do an example for the typical smiley face :-) . When you load the message from your database, you can do :


str_replace(':-)', '<img src="typicalSmiley.gif" />', $theMessage);

You need a line like this for each smiley you have.