Search code examples
javascriptjquerycursoronkeypress

How do I change cursor on keypress (Ctrl) in JQuery?


Using jQuery, I would like to change the cursor on page when any key is pressed (for example Ctrl). Simple task, however I can not get make it work... This was my first idea, obviously - does not work:

$(document).on('keydown',function(){
  $(document).css('cursor','wait');
});

Any idea what is wrong?


Solution

  • Think what you want is something like this:

    $('body').css('cursor','wait');
    

    You can't apply CSS to the document. Also if you wanted to only apply this css to the CTRL key press you could do:

    $(document).on('keydown', function (event) {
        if (event.ctrlKey) {
            $('body').css('cursor', 'wait');
        }
    });