2

I have a form with multiple TextBoxes, Textarea and Buttons. By pressing the ENTER in any text box submits the form. So i want to disable the pressing enter only in text boxes and to enable in text-area and buttons. i found a solution but they were using higher jquery version which were not working with mine jquery version. Below is the code i wrote but its only enables the enter in test area and not in buttons. I only have acces to jquery 1.3.1 or just java script. Please help me to fix this code.

$(document).keydown(function(e) {
    var element = e.target.nodeName.toLowerCase();
    var element1 = e.target;
    alert(element1);
    if (element != 'textarea') {
        if (e.keyCode === 13) {
            return false;
        }
    }
});

Here i do not want to completely disable pressing the ENTER. i want to disable it only in textboxes.

Nimble Fungus
  • 513
  • 3
  • 22

2 Answers2

6
$(function () {
  $('input[type=text]').keypress(function(event) {
    if (event.keyCode == 13) {
      event.preventDefault();
    }
  });
});

Js Fiddle : https://jsfiddle.net/bikrampahi/f0gxev76/

Nimble Fungus
  • 513
  • 3
  • 22
Bikram Pahi
  • 1,107
  • 1
  • 12
  • 33
0

Add the following attribute to the form element but only if there are no textareas on the form.

onkeypress="return event.keyCode != 13;"

Fiddle: https://jsfiddle.net/uffbost5/1/

Craig
  • 332
  • 1
  • 7