Change mouse cursor in Javascript or jQuery

60,394

Solution 1

Do it in both html and body:

$('html,body').css('cursor','crosshair');

http://jsfiddle.net/2Qffw/3/

Solution 2

It does work, but you had an empty body.

http://jsfiddle.net/z6mhH/

HTML

<body>
    asdasdasdasdads
</body>

JS

document.body.style.cursor = "crosshair";

Solution 3

document.getElementById("mydiv").style.cursor="move";
Share:
60,394

Related videos on Youtube

Arnaud
Author by

Arnaud

Updated on July 09, 2022

Comments

  • Arnaud
    Arnaud almost 2 years

    How can I change the mouse cursor in Javascript or in jQuery ? I know it's a very classic question, but very strangely this JSFiddle doesn't work : http://jsfiddle.net/2Qffw/.

    Here is the code :

    $('body').css('cursor','pointer');
    
    document.body.style.cursor = "pointer";
    
    • adeneo
      adeneo almost 11 years
      Try locally without jsFiddle, and it should work, or try an element in jsFiddle and it works as well -> jsfiddle.net/2Qffw/1
    • cirrus
      cirrus almost 11 years
      The nicest way of doing this is with a CSS rule using :hover. Can you model your app that way instead?
    • Arnaud
      Arnaud almost 11 years
      @adeneo : locally, it works without <!DOCTYPE html>, but not with.
    • Arnaud
      Arnaud almost 11 years
      @cirrus : it's not really my problem. In fact, I've largely reduced my problem to that I presented in my question.
    • Corion
      Corion almost 11 years
      This doesn't seem like something you should be doing with jQuery instead of plain old CSS.
    • JBH
      JBH over 6 years
      @Corion, not true. CSS only restricts the change to only the item over which you are pointing. That's capricious unless it's the body or HTML, then it's permanent. I'm here to find a way to change the cursor while a file is uploading, and then change it back when the upload is complete. That's not a CSS problem.