Replace default CTRL+F from browsers with a jQuery event

Asked

Viewed 127 times

1

Where I have tables on my system, in almost all used the plugin dataTables to customize, but my plugin ta a little modified.

In the modification in question, the search field inside the table is hidden, and I activate it with a 'activate search' button'.

What I want to do activate this search with the keyboard, preferably CTRL+F, do this is simple:

$(document).keydown(function (e) {
  if (e.keyCode == 70 && e.ctrlKey) {
     $("#dataTable_filter").find('.form-control').show().focus();
  }
});

But in this, it activates the default search of the browsers, and moves the focus to this other search, the doubt is:

There is how to disable this default browser search and use only the my trigger? Or you have to do it another way to get this one outworking ?

1 answer

1


Test add and.preventDefault();

$(document).keydown(function (e) {
  if (e.keyCode == 70 && e.ctrlKey) {
     $("#dataTable_filter").find('.form-control').show().focus();
     e.preventDefault();
  }
});

If it doesn’t work try using it this way:

window.addEventListener("keydown",function (e) {
    if (e.keyCode === 114 || (e.ctrlKey && e.keyCode === 70)) { 
        $("#dataTable_filter").find('.form-control').show().focus();
        e.preventDefault();
    }
})
  • 1

    I add two things: 1) it is not 100% safe to achieve the same effect in every browser; 2) it is extremely wrong, from the point of view of usability, to do this, since you modify a behavior that your user is already used to, and this will bring a bad perception on his part.

  • 1

    Your answer is right. I’m just warning @anthraxisbr to avoid replacing native browser functions with their own solutions. It causes discomfort and, in terms of UX, it’s terrible.

  • I marked your comments as important for him to read.

  • I had forgotten the default prevention, it worked right, regarding browser poratabilidade does not difença, it is an intranet system and everyone who uses owns all the programs in the same version on their Pcs.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.