Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

disable all keyup/keydown/keypressed events present on page

I am having plenty of key events here on my page.Is there any way to disable all the keyup/keydown/keypressed events present on page rather than disabling each event separately. i am looking for solution using javascript/jquery.

Thanks!!

like image 542
Milind Anantwar Avatar asked Mar 06 '13 10:03

Milind Anantwar


2 Answers

You could do it this way, though I expect it might be horrendously slow on larger pages:

$('*').off('keyup keydown keypress');

That's going to select every single element on the page, then remove any keyup, keydown, and keypress events that are bound to them.

If you want to prevent the user from using the backspace key to navigate to the previous page, you could try the following code:

var inputTags = ['INPUT', 'TEXTAREA'];

$(document).on('keypress', function(e) {
    if(e.which === 8 && $.inArray(e.target.tagName, inputTags) === -1)
        e.preventDefault();
});

That should restrict the use of the backspace key, except in instances where the focus is an input element where you can enter text (so an <input type="text"> or a <textarea>).

Take a look at this working demo.

like image 114
Anthony Grist Avatar answered Sep 24 '22 02:09

Anthony Grist


Try

$(document).find('*').off('keyup keydown keypressed');

and you should put this into the $(document).ready() block, after all the loaded JS on page (before </body> tag, for example).

like image 23
alexbusu Avatar answered Sep 21 '22 02:09

alexbusu