Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery set enter key textarea in My Code

I have an Jquery submit textarea, now I want to set textarea submit without button submit. Just using enter key.

<textarea id="ctextarea"></textarea>

Here it's the JS :

$('.comment_button').live("click",function() 
{
    var ID = $(this).attr("id");
    var uid = $("#uid").val();
    var comment= $("#ctextarea"+ID).val();
    var dataString = 'comment='+ comment + '&msg_id=' + ID + '&uid=' + uid;

    if(comment=='')
    {
    $('#ctextarea').html("").fadeIn('slow');
    $("#ctextarea"+ID).focus();
    }
    else if (!$.trim($("#ctextarea"+ID).val()))
    {
        $("#ctextarea"+ID).focus();
    }
    else
    {
    $.ajax({
    type: "POST",
    url: "comment_ajax.php",
    data: dataString,
    cache: false,
    success: function(html){
    $("#commentload"+ID).append(html);
    $("#ctextarea"+ID).val('');
    $("#ctextarea"+ID).focus();
     }
 });
}
return false;
});

I already search the tutorials and found, but I confused where can I put the code in My JS code.

Someone can give the idea ? Thanks for helps.

like image 863
Nagini Avatar asked Jan 18 '26 06:01

Nagini


1 Answers

$('#ctextarea').on('keyup', function(e){
    if(e.which == 13 || e.keyCode == 13){
        //enter key pressed.. 
    }
});
like image 92
Ohgodwhy Avatar answered Jan 20 '26 19:01

Ohgodwhy