smilies to textarea on click

后端 未结 4 969
盖世英雄少女心
盖世英雄少女心 2021-01-06 07:21

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

4条回答
  •  感情败类
    2021-01-06 07:41

    try also this...

    $('#emoticons a').click(function() {
    
        var smiley = $(this).attr('title');
    
        var cursorIndex = $('#description').attr("selectionStart");
        var lStr =  $('#description').val().substr(0,cursorIndex);
        var rStr = $('#description').val().substr(cursorIndex);
    
        $('#description').val(lStr+" "+smiley+" "+rStr);
    
    });
    

    Fix after your comment:

    $('#emoticons a').click(
    function(){var smiley = $(this).attr('title');
    
    var cursorIndex = $('#description').attr("selectionStart");
    var lStr =  $('#description').val().substr(0,cursorIndex) + " " + smiley + " ";
    var rStr = $('#description').val().substr(cursorIndex);
    
    $('#description').val(lStr+rStr);
    $('#description').attr("selectionStart",lStr.length);                                     
    });​
    

提交回复
热议问题