Search code examples
javascriptjqueryumbracopreventdefault

Add "preventDefault" to all labels that uses specific Umbraco template


I'm having an issue with one of my main templates in umbraco. The issue is that, whenever I'm logged in on my website and I hit the 'enter' key whilst focus is on an input label, I'm logged out and redirected to my startpage.

I know that the event.preventDefault() method will stop this, but I cannot figure out how to apply it in my scenario.

Is it possible to add a script to my Umbraco template that adds the "preventDefault()" in case of keyCode == 13 (the enter key)? And if so, how exactly?

I have a bunch of labels in all kinds of macros that uses this template, and I would very much prefer not to add these lines manually for each of them!

I have tried quite a lot of variation of the following, but without any luck:

  $("input").click(function(e)
  {
    if(e.keyCode == 13) {

    preventDefault();
    }
  });

Thank you for your time.


Solution

  • You check the keyCode on a click, that won't work. Also, the preventDefault() should be used on the event: e.preventDefault().

    Use this instead for global enter key:

    $(document).ready(function() {
      $(window).keydown(function(event){
        if(event.keyCode == 13) {
          event.preventDefault();
          return false;
        }
      });
    });