Open In App

How to change cursor to waiting state in JavaScript/jQuery ?

Improve
Improve
Like Article
Like
Save
Share
Report

Given an HTML document and the task is to get the waiting state cursor when the mouse moves over an element. Here we are going to achieve that by cursor property which allows doing an 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 a waiting cursor is needed.
  • Set its value to default when a standard cursor is needed.

The cursor can be changed by using the following syntax:

object.style.cursor = value

Example 1: 
 

html




<!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>


Output: 
 

Example 2: 
 

html




<!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>


Output: 
 

Example 3:

HTML




<!DOCTYPE html>
<html>
  <head>
    <title>Document</title>
    <style>
      #demo {
        width: 200px;
        height: 50px;
        padding: 25px;
        background-color: green;
        color: white;
        font-size: 15px;
      }
    </style>
  </head>
  <body>
    <h1>Welcome to GeeksForGeeks</h1>
    <p id="demo" onmouseover="myFunction()">
      Mouse over this text before and after you have clicked the button below!
    </p>
 
 
    <script>
      function myFunction() {
        document.getElementById("demo").style.cursor = "wait";
      }
    </script>
  </body>
</html>


Output:

In the above example we have used onmouseover property in <p> tag and it has called myFunction() method in <script> tag. We can use the wait property with the mouse cursor in JS and CSS throughout the webpage. We can set the cursor to wait using object.style.cursor = “wait” in javascript.



Last Updated : 08 Sep, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads