CSS or Javascript to disable text selection from CTRL + A

Note: this question is different from How to turn off text selection highlighting using CSS?

Before asking, I read the discussion history from the above question. Everything works fine, except that I can enable CTRL+A (Select All) only works inside input elites.

This is because I am deploying my HTML5 application to the desktop and want the same behavior from the GUI / Forms application.

What would be the starting point? Try to bind all the elements to the keypress event and observe CTRL + A keyCode ? The disadvantage of this approach would be to control everything and take care of repeated renders.

I prefer a CSS solution, but any idea is welcome.

Thanks in advance.

@EDIT: I found this ulgy solution, but it works:

 $(document).keydown(function(objEvent) { if (objEvent.ctrlKey) { if ((objEvent.keyCode === 65) || (objEvent.keyCode == 97)) { if ($(objEvent.target).not("input").disableTextSelect().length) { return false; } } } }); 
+4
source share
2 answers

Possible duplicate.

If you are allowed to use jquery, here is the answer you are looking for.

65 - ascii for 'A' and 97 for 'a' if you want to report to both.

 $(function(){ $(document).keydown(function(objEvent) { if (objEvent.ctrlKey) { if ($(objEvent.target).not('input')){ if (objEvent.keyCode == 65 || objEvent.keyCode == 97) { objEvent.preventDefault(); } } } }); }); 

edit: modified based on your comment to allow input.

edit 2: if you enabled the jQuery user interface, you can use disableSelection () instead of disableTextSelect () otherwise try this.

edit 3: Sorry, disableSelection () is deprecated in jQuery 1.9, see here. . Try this simple hack instead of disableSelection (). replace objEvent.disableTextSelect(); on objEvent.preventDefault(); above.

edit 4: made a little cleaner.

+5
source
 user-select:none; -moz-user-select: none; -webkit-user-select: none; -ms-user-select: none; 

Use these CSS properties to stop this behavior.

-3
source

Source: https://habr.com/ru/post/1482480/


All Articles