0

http://liveweave.com/tq7rwk

Is there anyway to dynamically unfocus (focus = false) a textbox or textarea element?

I tried triggering .blur and .focusut but those only apply when it is focused out. It won't trigger a focus out event.

Any help is greatly appreciated.

$(document).bind('keydown', 'ctrl+1', function() {
  alert("hotkey triggered");
  if ( $(".txtbox").is(":focus") ) {
    $(".txtbox").trigger('focusout');
  }
});
Michael Schwartz
  • 8,153
  • 14
  • 81
  • 144

2 Answers2

0

just use:

$('.txtbox').blur();

to set the focus use: .focus()

or detect the keyCode:

$('input').on('keydown', function(e) {
    if(e.ctrlKey && e.keyCode == 81) { // if Ctrl+Q (dont use Ctrl+1, in chrome its jump to tab 1)
        $(this).blur();
    }
});

http://jsfiddle.net/99SP4/2/

meni181818
  • 1,103
  • 1
  • 7
  • 11
  • I already tried this as stated in my original post, and it does not seem to work with the jquery.hotkeys library I'm using. – Michael Schwartz Aug 05 '14 at 22:11
  • @mikethedj4, see my update. list of keyCodes here: http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes – meni181818 Aug 05 '14 at 22:17
0

I had to use the shortcut.js Javascript library by Binny V A.

What's great about this library is you can use a key combination like CTRL+T (which in most web browsers is a native hotkey to make a new tab. This script will allow you to use your custom hotkey function and will not call the browser's custom hotkey.

shortcut.add("Ctrl+N", function() {
  alert("Called new document");
});

Here's the new revised fiddle: http://liveweave.com/GWjbUc

Michael Schwartz
  • 8,153
  • 14
  • 81
  • 144