Xanger
Xanger

Reputation: 67

Disable shortcut in textarea and input

I have this javascript that allows me to place the shortcut, but I want to deactivate them in the textarea and input

//press A for back to top
jQuery(document).keydown(function(e){
    var target = e.target || e.srcElement;
    if ( target.tagName !== "TEXTAREA" || target.tagName === "INPUT" ) {
            if(e.which == 84) {
                $("html, body").animate({ scrollTop: 0 }, 500);
                return false;
            }
    }
});

but because if I insert ( target.tagName !== "TEXTAREA" || target.tagName === "INPUT" ) the script does not work? how do I fix?

Upvotes: 0

Views: 123

Answers (2)

Luca
Luca

Reputation: 1626

You can simplify you code to

//press A for back to top
$(document).keydown(function(e){
console.log(e);
    if(e.which == 65 && e.target.tagName !== 'INPUT'&& e.target.tagName !== 'TEXTAREA') {
      $("html, body").stop().animate({ scrollTop: 0 }, 500);
      return false;
    }
});

By the way in your comment you wrote "press A to top" but your code use "t" charcode (84); correct charcode is 65.

Here a working jsfiddle

Upvotes: 0

Andrey
Andrey

Reputation: 1553

You have a wrong condition, the right one is:

if ( target.tagName != "TEXTAREA" && target.tagName != "INPUT" ) {...}

Also the Unicode value of 'A' is 65, 84 is for 'T'.

Upvotes: 1

Related Questions