Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery key press event on textarea

Tags:

html

jquery

My html code for textarea is as below.

 <div class="controls">
                        <asp:TextBox ID="txtContent" TextMode="multiline" Columns="75" Rows="5" runat="server"
                            ToolTip="Content" data-val-required="Content is required." Width="154px" data-val="true"
                            Style="width: 400px; height: 75px; resize: none;"> </asp:TextBox>
                        <span class="field-validation-valid text-warning red" data-valmsg-for="txtContent"
                            data-valmsg-replace="true"></span>
                    </div>

I have a save button to save the details enetered in the above textarea. All these are given in a popup

 <button id="lnkSaveWSR" title="Save" class="btn btn-small btn-success"
onclick="AddWSRActivity();"
                runat="server">
                <i class="icon-ok"></i>Save
            </button>

I have written my jquery code as below.

$(document).keypress(function (e) {
            var code = (e.keyCode ? e.keyCode : e.which);
            if (code == 13) {
                if (!$('#form1').valid()) {
                    return false;
                }
                WSR.content = $('#txtContent').val();
                WSR.CategoryId = $('#ddlAddWSRCategory').val();
                var WSRAddUrl = ISM_Web_Path + '/WSR/WSRHandler/SaveActivities/' + ISMAjaxHandlerName;
                $.post(WSRAddUrl, { WSRInfo: $.toJSON(WSR) },
                                      function (data) {
                                          parent.ShowSuccessMessage(ISM_JS_Constants.Global.Data_Add_Suc_Msg);
                                          parent.$('#AddWSRModal').modal('hide');
                                          parent.RefreshWSRGrid();
                                      });
            }
        });

When I press enter key after entering text in textarea the event is not getting triggered. instead cursor is going to next line in the textarea.

Please help me in fixing the issue. Thanks in advance.

like image 950
Mythily Avatar asked Dec 30 '25 17:12

Mythily


1 Answers

I think you can just listen event on textarea

$("#txtContent").on("keydown", function(e){
  if(e.which == 13){
    // your code
    return false;
  }
});

PS: don't listen event in document.

like image 53
Farkhat Mikhalko Avatar answered Jan 01 '26 10:01

Farkhat Mikhalko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!