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.

14
  • 1
    FYI, that's an extremely inefficient way of binding that event. For something like this, it's best to find the common parent of all inputs and assign the event there... Commented Dec 16, 2016 at 14:34
  • @acarroz5 the marked question wnats to disable completely. That dosen't solve my problem. its a different question. Please help Commented Dec 16, 2016 at 14:37
  • how are you submitting the form? Via an AJAX call or via a php script? Commented Dec 16, 2016 at 14:38
  • When textboxes have focus (give them a class) you could do a preventDefault() on the form submission? Commented Dec 16, 2016 at 14:40
  • 1
    There has to be something very special in your question, if you can't apply e.preventDefault() to your code. I just can't see the special nature of the question ... Commented Dec 16, 2016 at 14:42

2 Answers 2

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

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

Sign up to request clarification or add additional context in comments.

7 Comments

Bikram its not stoping the enter not even in Text Boxes. I have a jquery version 1.3.2. can that be the limitation?
an ALERT inside this code is not getting called when i am putting ENTER in text area.
I have implemented this way many time. if it is not working with your jquery version then I will check and update my answer in few minute.
its not even called.
I added ALERT in the code and its not working. Alert is not poping up up.jsfiddle.net/f0gxev76/1
|
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/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.