0

It is possible to get the current cursor type without predefined cursor style, like when the mouse pass over text, link..

Something like that :

document.addEventListener('mouseover', (e) => {
  console.log(e.'cursorType')
});

I would like to get in the console.log the state of the cursor like : pointer, text, move, wait...

I find some kind of solution in jQuery but I am looking for one in pure vanilla JS

Thank for your answer

  • Does this answer your question? [how to use javascript get current cursor style from browser](https://stackoverflow.com/questions/38762367/how-to-use-javascript-get-current-cursor-style-from-browser) – MertDalbudak Mar 20 '21 at 15:00

3 Answers3

3

Since it may not have been defined inline, you need the computed style:

I use Click here, because it is easier to demo

document.addEventListener('click', e => {
  const tgt = e.target;
  const inline = tgt.style.cursor || "Not defined"
  const computed = window.getComputedStyle(tgt)["cursor"]
  console.log("Inline: ",inline,"Computed: ",computed)
});
.help { cursor: help }
<span style="cursor:pointer">Inline Pointer</span>
<hr>
<span class="help">CSS help</span>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
0

You could get it from the CSS property "cursor" like this:

document.addEventListener('mouseover',function(e){
    var cursor = e.target.style.cursor;
    console.log(cursor);
},false);
Agustin
  • 67
  • 9
0

Try logging e.target.style.cursor. I think that will give you the cursor type on mouseover of a DOM element.

Robert Lin
  • 369
  • 3
  • 9