Nimble Fungus
Nimble Fungus

Reputation: 548

Disable Enter to submit the form

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.

Upvotes: 2

Views: 986

Answers (2)

Bikram Pahi
Bikram Pahi

Reputation: 1193

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

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

Upvotes: 6

Craig
Craig

Reputation: 332

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/

Upvotes: 0

Related Questions