JavaScript Catch keypress document wide and start typing in search box

I want to catch all key presses on my webpage so that when you start typing the text appears in the search box (input) and this box gets focus.

Can someone provide me some hints?

Answer:1

Solution

Since you tagged your question with jquery, here you go:

var $search = $("#search");

$(document).on("keydown", function(e) {
  if (!$search.is(":focus"))
    if (e.which != 9 && e.which != 13)
      $search.focus();
});

The .focus() function to set the focus to the input field won't be called if the focus is already set. The input field also won't be selected if the user pressed the tab or enter key!

Accessibility

If someone tries to use your website without a mouse (just with keyboard), he won't be able to navigate anywhere. Maybe you have to consider to use additional techniques like the HTML's tabindex.

Answer:2

You can use jquery like below

$(document).keydown(function(event) {
    $("id-of-input-box").focus()
});
Answer:3

I have two classes in jquery e.g: function a(){ this.init = function(a){} } function b(){ this.init = function(a){} } Both the classes have this.init() method.I have a situation that i have the ...

I have two classes in jquery e.g: function a(){ this.init = function(a){} } function b(){ this.init = function(a){} } Both the classes have this.init() method.I have a situation that i have the ...

  1. get object name jquery
  2. object class name jquery
  3. json object name jquery
  4. jquery object name variable
  5. find object by name jquery
  6. jquery object name to string
  7. select object by name jquery
  8. get object key name jquery
  9. get object property name jquery
  10. get object class name jquery
  11. jquery object name convention
  12. jquery object name value
  13. jquery object name 取得

I have a problem with JavaScript callback functions. I'm using node Js and node-mysql to process mysql queries. When new users registered i need to do 2 db checks if This email already registered and ...

I have a problem with JavaScript callback functions. I'm using node Js and node-mysql to process mysql queries. When new users registered i need to do 2 db checks if This email already registered and ...

  1. javascript process function

According to this link by mozilla, the data- attribute is used for setting data in the DOM. I want to grab the time using new Date().getTime() and append it to an element in the DOM. What is the ...

According to this link by mozilla, the data- attribute is used for setting data in the DOM. I want to grab the time using new Date().getTime() and append it to an element in the DOM. What is the ...

This site is probably overkill for the level of technical input I need for my query but the only other option is to ask Weebly support who are useless. Essentially I have a tracking pixel from google ...

This site is probably overkill for the level of technical input I need for my query but the only other option is to ask Weebly support who are useless. Essentially I have a tracking pixel from google ...

  1. adding tracking pixel to email
  2. adding tracking pixel to html
  3. adding a tracking pixel to website
  4. shopify adding a tracking pixel