locked
Exclude Multiline textbox from Onkeypress 13 Enter Form Submit. RRS feed

  • Question

  • User-666661156 posted

    Hey Everyone! 

    I am submitting the form when ENTER is pressed within any controls. But I want to exclude this behavior from multiline textboxes and preserve the default behavior or moving on to the next line when ENTER is pressed. I have had no luck in achieving this. Here's my code: 

            $("#Form").keydown(function (e) {
                var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
                if (keyCode == 13) {
                    $("#btnSubmit").add("#btnUpdate").click();
                    return false;
                }
            });

    The above code works as intended, I've tried

                if (keyCode == 13 && e.delegateTarget.nodeName != "#txtMulti") 
    

    Which didn't work, I was able to use e.PreventDefault() for the multi line txtMulti, but that completely disables ENTER within the textbox, so you won't be able to move to the next line either when you hit ENTER. 

    Appreciate any help on this! 

    Friday, September 16, 2016 1:18 AM

Answers

  • User-1496088595 posted

    if it is multiline textbox then it will be rendered as teaxtarea in html, so based on type you can skip the submit.

    $(e.target).is('textarea')

    $("#Form").keydown(function (e) {
       var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
       if (keyCode == 13 && !$(e.target).is('textarea')) {
          $("#btnSubmit").add("#btnUpdate").click();
          return false;
       }
    });

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 16, 2016 6:32 AM

All replies

  • User-1496088595 posted

    if it is multiline textbox then it will be rendered as teaxtarea in html, so based on type you can skip the submit.

    $(e.target).is('textarea')

    $("#Form").keydown(function (e) {
       var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
       if (keyCode == 13 && !$(e.target).is('textarea')) {
          $("#btnSubmit").add("#btnUpdate").click();
          return false;
       }
    });

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 16, 2016 6:32 AM
  • User-666661156 posted

    Thnx HemantD, 

    That worked!

    Friday, September 16, 2016 12:19 PM