0

I have a form in which a user needs to fill the fields. As only two fields are the required fields, so when user fills the required fields and press enter(windows)/return(mac) key the form gets submitted. I'm trying to disable the enter/return key function. So, for this I tried to inject a jQuery within the form.

my jQuery is as follows:

(function ($) {
Drupal.behaviors.YOURTHEMENAME = {
attach: function(context, settings) {
$("input").keypress(function (evt) {
var enterCode = evt.charCode || evt.keyCode;
if (enterCode  == 13) { return false;
}
});
  }
});
}
};
})(jQuery);

The jQuery part is familiar to me, but it doesn't work in the site. And the form responds to enter/return key press.

Is there any other way to disable enter/return key press. something like a module..

2 Answers 2

0

You need to use evt.preventDefault();

I wrote a blog post on making this work with drupal and ajaxified forms, which may let you see a slightly larger example including Drupal.behaviors.

18
  • In "input.some-identifier", what is some-identifier here?? is that any identifier name of a div, that we are supposed to call that action??
    – GIRI
    Jul 8, 2014 at 0:36
  • The example on my blog is written to loop through many elements to make them behave in a spreadsheet-type fashion, so "input.some-identifier" is how the multiple form elements were identified. In other words, it is a JQuery selector.
    – coreyp_1
    Jul 8, 2014 at 0:57
  • so, I followed the guidelines. cleared cache, but there was no effect. form responds as submit when enter key pressed
    – GIRI
    Jul 8, 2014 at 1:00
  • can you show the code that you are using now?
    – coreyp_1
    Jul 8, 2014 at 1:03
  • I'm using the same code that was mentioned in the blog post except the for loop.
    – GIRI
    Jul 8, 2014 at 1:05
0

Resolved the issue. Added the Query using JS injector.

(function ($) {
Drupal.behaviors.YOURTHEMENAME = {
attach: function(context, settings) {
$('#id').keypress(function(event) {
   if (event.keyCode == '13') {
       event.preventDefault();
   }
});
}
};
})(jQuery);

return/enter key press is now disabled.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.