Related Articles

Related Articles

How to change cursor to waiting state in JavaScript/jQuery ?
  • Last Updated : 05 Sep, 2019

Given an HTML document and the task is to get the waiting state cursor when mouse moves over an element. Here we are going to achieve that by cursor property which allows to do operation on the cursor. We are going to do that with the help of JavaScript.

Approach:

  • Use the cursor property.
  • Set its value to progress when waiting cursor is needed.
  • Set its value to default when standard cursor is needed.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML>  
<html>  
  
<head
    <title
        How to change cursor to waiting
        in JavaScript/jQuery ?
    </title>
      
    <script src=
    </script>
      
    <style>
        #div {
            height: 100px;
            width: 200px;
            background: green;
            color: white;
            margin: 0 auto;
        }
    </style>
</head
      
<body style = "text-align:center;">  
      
    <h1 id = "h1" style = "color:green;" >  
        GeeksforGeeks
    </h1>
      
    <p id = "GFG_UP" style
        "font-size: 15px; font-weight: bold;">
    </p>
      
    <div id = "div">
        Hover over it
    </div>
      
    <script>
        var el_up = document.getElementById("GFG_UP");
        var heading = document.getElementById("h1");
        var div = document.getElementById("div");
        el_up.innerHTML = "Hover over the element "
                    + "to see the waiting cursor.";
          
        $("#div").hover(function() { 
            $(this).css("cursor", "progress"); 
        }, function() { 
            $(this).css("cursor", "default"); 
        });  
    </script
</body>
  
</html>

chevron_right


Output:

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML>  
<html>  
  
<head
    <title
        How to change cursor to waiting
        in JavaScript/jQuery ?
    </title>
      
    <script src=
    </script>
      
    <style>
        #div {
            height: 100px;
            width: 200px;
            background: green;
            color: white;
            margin: 0 auto;
        }
        .cursor {
               cursor: progress;
           }
    </style>
</head
      
<body style = "text-align:center;">  
      
    <h1 id = "h1" style = "color:green;" >  
        GeeksforGeeks
    </h1>
      
    <p id = "GFG_UP" style
        "font-size: 15px; font-weight: bold;">
    </p>
      
    <div id = "div">
        Hover over it
    </div>
      
    <script>
        var el_up = document.getElementById("GFG_UP");
        var heading = document.getElementById("h1");
        var div = document.getElementById("div");
        el_up.innerHTML = "Hover over the element "
                    + "to see the waiting cursor.";
          
        $("#div").hover(function() { 
            $(this).addClass('cursor'); 
        }, function() { 
            $(this).removeClass('cursor');
        });  
    </script
</body>  
  
</html>

chevron_right


Output:

full-stack-img




My Personal Notes arrow_drop_up
Recommended Articles
Page :