3
votes

I want to call insert_comment() function when user press enter in textarea: Same as facebook.

<form action="" method="post" enctype="multipart/form-data" name="comment_form" id="comment_form">
<tr>
<td colspan="3" align="left" valign="top"><textarea name="comment_text" id="comment_text_<?php echo $i; ?>"  class="valid Post_Description_Text" placeholder="Write a comment here..."></textarea></td>
</tr>
<tr>
<td colspan="3" align="right" valign="top"><span id="extra_small_round_loader_<?php echo $postID; ?>"></span>&nbsp;<input type="button" name="comment" value="Comment" id="comment" onclick="insert_comment(<?php echo $i; ?>);" /></td>
</tr>
</form>

And here is the function insert_comment() i want to call on press enter in textarea:

<script type="text/javascript">
function insert_comment(id)
{
    var comment_des         = $("#comment_text_"+id).val();
    var fk_post_id          = $("#fk_post_id_"+id).val();
    var post_user_id        = $("#post_user_id_"+id).val();
    var user_id             = $("#user_id_"+id).val();

        $.post('ajax_files/insert_comment.php', {comment_des:comment_des,user_id:user_id,fk_post_id:fk_post_id,post_user_id:post_user_id},
        function(data) 
        {
            //$("#ajaxdata").html(data);
            $("#extra_small_round_loader_"+fk_post_id).empty().html('<img src="images/extra_small_round_loader.gif">');
            $('#reload_posts').load('ajax_files/reload_posts.php');


        });
}
</script>

Solved : Ok everyone i have find a solution, i tested it and its working perfect.

Here is the Textarea Field :

<textarea name="comment_text" id="comment_text_<?php echo $i; ?>"  class="valid Post_Description_Text" placeholder="Write a comment here..." onkeyup="enter_comment(event,<?php echo $i; ?>);"></textarea>

And there is the Function:

<script type="text/javascript">
function enter_comment(event,id) {
       if (event.which == 13) {
           insert_comment(id); // Call any function here: Just pass your actual Parameters to enter_comment().
       }

}
</script>
2

2 Answers

1
votes

Try adding an onkeyup handler to your textarea:

onkeyup="if(event.keyCode==13) insert_comment(<?php echo $i; ?>);"
0
votes
$(".Post_Description_Text").keypress(function(event) {
       if (event.which == 13) {        
            insert_comment(/((0-9)+)/.exec($(this).attr(id) )[0] );
         }

});
 You can check if the enter key is pressed usinge event.which. Keycode for enter is 13. The regex is just to extract the post id from the id of textbox