I have a little question
I want to click on my smileys and insert the text to the textarea. and when I want to add a smiley later, the smiley should be added to the cursor position and not at the end in the textarea.
This is my html code:
<textarea id="description" name="description"></textarea>
<div id="emoticons">
<a href="#" title=":)"><img alt=":)" border="0" src="/images/emoticon-happy.png" /></a>
<a href="#" title=":("><img alt=":(" border="0" src="/images/emoticon-unhappy.png" /></a>
<a href="#" title=":o"><img alt=":o" border="0" src="/images/emoticon-surprised.png" /></a>
</div>
This is my JS code:
$('#emoticons a').click(function(){
var smiley = $(this).attr('title');
$('#description').val($('#description').val()+" "+smiley+" ");
});
Here you can see the result (NO WRAP - BODY) http://jsfiddle.net/JVDES/8/
I use an extern JS file for my javascript-code...
Do you know why the code isn't running in NO WRAP - HEAD mode?
http://jsfiddle.net/JVDES/9/
Thanks for helping
Regards bernte
try also this...
Fix after your comment:
Also, you can use something this function:
For your example, look here.
UPD 1:
To set cursor at specified position, use the next function:
I updated code on jsFiddle, so, to view new example, look here.
The difference between
no wrap (head)
andno wrap (body)
is that the former will run the code as soon as possible and the latter will run the code after the page has loaded.In this case, with
no wrap (head)
, the code is being run before '#emoticons a' exists. Thus $('#emoticons a') returns none and does not attach the click handler. Later, the link is created.Thus, the solution is to tell the code to run when the page is loaded.
There are a couple, equivalent ways of doing this. http://api.jquery.com/ready/
$(document).ready(handler)
$().ready(handler)
(this is not recommended)$(handler)
So using version 3, we have:
http://jsfiddle.net/Nc67C/
Try this - http://jsfiddle.net/JVDES/12/
Credit for the caret position function goes to - https://stackoverflow.com/a/1891567/981134