i have comments system, where user have reply button on every comment. after reply click he gets new form with textarea. in textarea i have smyles. after user clicks on smile, smyle code inserts in textarea where cursor is focused.
problem is, when i toggle smyles button and show again, smile inserts tripple times.
i crated jsfiddle basic example. link
- click on "showsmiles" link in textarea
- click on smile.
- toggle "showsmiles"
- click again on smile ( here smile is now tripple )
code:
$(document).on('click','.showsmiles',function(){
var $smiles = $(this).next();
$smiles.toggleClass('displaysmiles');
$smiles.click('a',function(e){
e.preventDefault();
var $that = $(this);
var txtarea = $that.closest('.cont').find('textarea');
var caretPos = txtarea[0].selectionStart;
var textAreaTxt = txtarea.val();
var txtToAdd = ' :'+$that.data('alt')+': ';
txtarea.val(textAreaTxt.substring(0, caretPos) + txtToAdd + textAreaTxt.substring(caretPos) );
});
});