Changing cursor to waiting in javascript/jquery

JavascriptJqueryHtml

Javascript Problem Overview


enter image description here

How would i get my cursor to change to this loading icon when a function is called and how would i change it back to a normal cursor in javascript/jquery

Javascript Solutions


Solution 1 - Javascript

In your jQuery use:

$("body").css("cursor", "progress");

and then back to normal again

$("body").css("cursor", "default");

Solution 2 - Javascript

A colleague suggested an approach that I find preferable to the chosen solution here. First, in CSS, add this rule:

body.waiting * {
    cursor: progress;
}

Then, to turn on the progress cursor, say:

$('body').addClass('waiting');

and to turn off the progress cursor, say:

$('body').removeClass('waiting');

The advantage of this approach is that when you turn off the progress cursor, whatever other cursors may have been defined in your CSS will be restored. If the CSS rule is not powerful enough in precedence to overrule other CSS rules, you can add an id to the body and to the rule, or use !important.

Solution 3 - Javascript

Please don't use jQuery for this in 2021! There is no reason to include an entire external library just to perform this one action which can be achieved with one line:

Change cursor to spinner: document.body.style.cursor = 'wait'

Revert cursor to normal: document.body.style.cursor = 'default'

Solution 4 - Javascript

jQuery:
$("body").css("cursor", "progress");

back again
$("body").css("cursor", "default");

Pure:
document.body.style.cursor = 'progress';

back again
document.body.style.cursor = 'default';

Solution 5 - Javascript

The following is my preferred way, and will change the cursor everytime a page is about to change i.e. beforeunload

$(window).on('beforeunload', function(){
   $('*').css("cursor", "progress");
});

Solution 6 - Javascript

$('#some_id').click(function() {
  $("body").css("cursor", "progress");
  $.ajax({
    url: "test.html",
    context: document.body,
    success: function() {
      $("body").css("cursor", "default");
    }
  });
});

This will create a loading cursor till your ajax call succeeds.

Solution 7 - Javascript

Using jquery and css :

$("#element").click(function(){ 
    $(this).addClass("wait");
});​

HTML: <div id="element">Click and wait</div>​

CSS: .wait {cursor:wait}​

Demo here

Solution 8 - Javascript

Override all single element

$("*").css("cursor", "progress");

Solution 9 - Javascript

You don't need JavaScript for this. You can change the cursor to anything you want using CSS :

selector {
    cursor: url(myimage.jpg), auto;
}

See here for browser support as there are some subtle differences depending on browser

Solution 10 - Javascript

Here is something else interesting you can do. Define a function to call just before each ajax call. Also assign a function to call after each ajax call is complete. The first function will set the wait cursor and the second will clear it. They look like the following:

$(document).ajaxComplete(function(event, request, settings) {
    $('*').css('cursor', 'default');
  });

function waitCursor() {
    $('*').css('cursor', 'progress');
  }

Solution 11 - Javascript

If it saves too fast, try this:

<style media="screen" type="text/css">
    .autosave {display: inline; padding: 0 10px; color:green; font-weight: 400; font-style: italic;}
</style>

<input type="button" value="Save" onclick="save();" />&nbsp;
<span class="autosave" style="display: none;">Saved Successfully</span>


$('span.autosave').fadeIn("80");
$('span.autosave').delay("400");
$('span.autosave').fadeOut("80");

Solution 12 - Javascript

Setting the cursor for 'body' will change the cursor for the background of the page but not for controls on it. For example, buttons will still have the regular cursor when hovering over them. The following is what I am using:

To set the 'wait' cursor, create a style element and insert in the head:

var css = "* { cursor: wait; !important}";
var style = document.createElement("style");
style.type = "text/css";
style.id = "mywaitcursorstyle";
style.appendChild(document.createTextNode(css));
document.head.appendChild(style);

Then to restore the cursor, delete the style element:

var style = document.getElementById("mywaitcursorstyle");
if (style) {
  style.parentNode.removeChild(style);
}

Solution 13 - Javascript

I found that the only way to get the cursor to effectively reset its style back to what it had prior to being changed to the wait style was to set the original style in a style sheet or in style tags at the start of the page, set the class of the object in question to the name of the style. Then after your wait period, you set the cursor style back to an empty string, NOT "default" and it reverts back to its original value as set in your style tags or style sheet. Setting it to "default" after the wait period only changes the cursor style for every element to the style called "default" which is a pointer. It doesn't change it back to its former value.

There are two conditions to make this work. First you must set the style in a style sheet or in the header of the page with style tags, NOT as an inline style and second is to reset its style by setting the wait style back to an empty string.

Solution 14 - Javascript

css:

html.wait, html.wait * { cursor: wait !important; }

on click:

onclick: "myFunction();"  //call your procedure

separate cursor change right before onclick by:

onmousedown="$('html').addClass('wait');"

at end of your function:

$('html').removeClass('wait');" //back to normal cursor

or better timeout the cursor restore at beginning of your function: (restores cursor 50ms after your function ended doing stuff)

setTimeout(function() { $('html').removeClass('wait') }, 50);

ALL TOGETHER:

<div style="cursor: pointer" onmousedown="$('html').addClass('wait');" onclick="sample('DE');">sample</div>

<script>
    function sample(c) {
    	//timeout (this waits 50ms after function completed to restore cursor)
        setTimeout(function() { $('html').removeClass('wait') }, 50);
    
        //do your stuff including a calling opening a modal window
        //call ajax etc,
    
    }
</script>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionahmetView Question on Stackoverflow
Solution 1 - JavascriptStanleyView Answer on Stackoverflow
Solution 2 - JavascripthrabinowitzView Answer on Stackoverflow
Solution 3 - Javascriptkchuang7View Answer on Stackoverflow
Solution 4 - Javascriptxoxn-- 1'w3k4nView Answer on Stackoverflow
Solution 5 - JavascriptshaneparsonsView Answer on Stackoverflow
Solution 6 - JavascriptKanishka PanamaldeniyaView Answer on Stackoverflow
Solution 7 - JavascripttusarView Answer on Stackoverflow
Solution 8 - Javascriptสุดเขต นะจ๊ะView Answer on Stackoverflow
Solution 9 - JavascriptManseView Answer on Stackoverflow
Solution 10 - JavascriptShawn VincentView Answer on Stackoverflow
Solution 11 - JavascriptMark EilenbergerView Answer on Stackoverflow
Solution 12 - Javascriptcat_in_hatView Answer on Stackoverflow
Solution 13 - JavascriptDavid CrawfordView Answer on Stackoverflow
Solution 14 - Javascriptuser2033838View Answer on Stackoverflow