javascript - How can I get the element near the cursor? -


just said in title.i want target element near cursor.

 <style type="text/css">    span{        display: inline-block;        min-width: 80px;        border-bottom: 2px solid black;    }  </style>   <div contenteditable="true">   <span></span>   <span></span>   <span></span> </div> 

when users input words, want know curson in.

you this:

// span-elements var elements = document.getelementsbyclassname("span_element");  // cycle through them , add onmouseover-function them for(var = 0, length = elements.length; < length; i++) {     elements[i].onmouseover = function() {         console.log(this.id);     }  } 

therefor html should example (to adress elements):

<div contenteditable="true">   <span id="span1" class="span_element"></span>   <span id="span2" class="span_element"></span>   <span id="span3" class="span_element"></span> </div> 

side note: jquery more easier, have .focus().

side note 2: please have in mind, there difference between mousecursor (as described in initial post , answer) , caret position (where user typing). can different.


Comments