Suppose I have a jQuery
file where I have defined a key press action. And in my aspx
page I want to override that key press action.
How can I achieve that?
Asked
Active
Viewed 129 times
0

Code Painters
- 7,175
- 2
- 31
- 47

Divya
- 979
- 3
- 13
- 18
-
Sorry, can you explain better if you want to suppress your keypress handler or the event itself? – MarcoL Feb 13 '14 at 11:14
-
Hi Marco, I have the following code in a jquery file – Divya Feb 13 '14 at 11:18
-
Hi Marco, I have the following code in a jquery file -- $(document).ready(function () { $(document).mapHotKeys( [ { key: 'shift+F2', action: function () { alert('Blocked by hotkeys.'); } } ] ); }); And this actions I want to over write at my page – Divya Feb 13 '14 at 11:27
3 Answers
0
var input = '#txtId'; //txtid is ur textbox,textarea id
$(input).keypress(function(e) {
e.preventDefault();
});

Amarnath R Shenoy
- 5,121
- 8
- 24
- 32
0
Imagine you have this scenario:
var handler = function(){ ... }
$('body').on('keypress', ':input', handler);
Then you can remove that specific handler - with no impact on the others - with the following:
$('body').off('keypress', ':input', handler);
To remove ALL keypress handlers just call it without passing the handler
$('body').off('keypress', ':input');
To remove all event handlers from a specific element just use it without arguments
$(':input').off();
Instead if you want simply prevent the default behaviour, just use the preventDefault
function
$('body').on('keypress', ':input', function(e){
e.preventDefault();
...
});
In case instead you wat to use your handler just once there's the once
function in jQuery that will bind your handler and unbind it at the end of the execution;
Sorry for the long answer but your question wasn't completely clear to me.
Documentation link for jQuery off()

MarcoL
- 9,829
- 3
- 37
- 50
-
Hi Marco, my scenario was this ---I have written some global key press events which will be common in all the pages. Now I want to over write that a key press event in global events with an another event. Suppose in the global event for key "Ctrl" i have action as "Alert('Hi')"; So in one of the page I want to over write this as "Alert('Hello'); How can I achieve that? – Divya Feb 13 '14 at 11:53
-
I guess you can follow the first scenario I wrote, the one with the handler, then register a new handler with the behavior you want. Now the problem might be: how can you get the handler if it's in another file? A different solution could be instead to rewrite your jquery stuff in the file to work differently based on some parameter you can define in each page and that it could access - perhaps a global variable is the easiest thing. – MarcoL Feb 13 '14 at 12:07
-
If you want to try the way of "deregistering the handler", make sure you can reach the handler in some way - also here the easiest, but not best, choice could be to define it in the global namespace. Otherwise a pretty hard solution could be to check which handlers are registered in jQuery for that event and get the one you are looking for: http://stackoverflow.com/questions/2518421/jquery-find-events-handlers-registered-with-an-object – MarcoL Feb 13 '14 at 12:09