Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

detect cursor type

I want JavaScript code to detect the mouse cursor type.

For example when the cursor hovers in <textarea> it changes from default to text.

How would I go about detecting this?

like image 287
mkotwd Avatar asked Mar 14 '11 21:03

mkotwd


People also ask

How do I find my cursor?

Once you're in Mouse settings, select Additional mouse options from the links on the right side of the page. In Mouse Properties, on the Pointer Options tab, at the bottom, select Show location of pointer when I press the CTRL key, and then select OK. To see it in action, press CTRL.

Which is different cursor type?

There are 2 types of Cursors: Implicit Cursors, and Explicit Cursors. These are explained as following below.

How do I change the cursor type in HTML?

You can simply use the CSS cursor property with the value pointer to change the cursor into a hand pointer while hover over any element and not just hyperlink. In the following example when you place the cursor over the list item, it will change into a hand pointer instead of the default text selection cursor.


2 Answers

You could do this, but its not pretty, and will probably be quite slow depending on how many elements you have on your page.

$('*').mouseenter(function(){
    var currentCursor = $(this).css('cursor') ;

    //do what you want here, i.e.
    console.log( currentCursor );
});
like image 144
P4ul Avatar answered Sep 27 '22 17:09

P4ul


You can detect the cursor type using JavaScript

like

<input id="sample_text" name="one" type="text" value="Sample Text" />

and the JavaScript code should look something like this

$('input[id=sample_text]').click( function() {
   alert("test");   
   var ctl = document.getElementById('sample_text');
   var startPos = ctl.selectionStart;
   var endPos = ctl.selectionEnd;
   alert(startPos + ", " + endPos);
});

you can also look at this Jsfiddle for Js Cursor Detection

the above is the Jquery code written , you can also use the Vanilla JS for that you just need to change it to

<input id="sample_text" name="one" type="text" value="Sample Text" onclick="detect_cursor()" />

and the JavaScript should look something like this

function detect_cursor() {
   alert("test");   
   var ctl = document.getElementById('sample_text');
   var startPos = ctl.selectionStart;
   var endPos = ctl.selectionEnd;
   alert(startPos + ", " + endPos);
};
like image 39
Chitrank Dixit Avatar answered Sep 27 '22 16:09

Chitrank Dixit