Search code examples
javascripteventscursordom-eventsmouseevent

Detecting mouse cursor type change over element


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


Solution

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