How to catch the element where my cursor is?

Asked

Viewed 1,301 times

7

I’m creating some functions for the text editor Alloyeditor. Then I came across the need to acquire the element in Jquery that the cursor is present. I just don’t know if there is any way through Jquery to get the html element that is my cursor, or at least what html element my mouse clicked. Is there any way to do that? If so, how?

3 answers

8


My answer is limited to this:

"- at least which html element my mouse clicked on"

Theory: capture the event click throughout the body of the site; return the element clicked on this event through the property .target.

Practising:

$('body').on('click', function(event){
    let elementoClicado = $(event.target);
    elementoClicado.css('background-color', '#ddd');
    console.log(elementoClicado); // objeto jQuery!
    console.log(event.target); // objeto html
});
<!DOCTYPE html>
<html>
    <head>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    </head>
    <body>
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
        tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
        quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
        consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
        cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
        proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sunt, eum, iusto. Magnam reprehenderit praesentium et aperiam consectetur. Deleniti nihil minus, architecto, molestiae rem amet placeat odio numquam dolorum pariatur deserunt!</p>
    </body>
</html>


Although it doesn’t seem very functional, you can try using the same technique with the hover. But I’m telling you: it’s not very nice. Maybe you can adapt to your use defining well a selector. But here it comes:

$('*').hover(
    function(event){ // Mouse entrou no elemento
        let elementoHover = $(event.target);
        elementoHover.css('background-color', '#ddd');
    },
    function(event){ // Mouse saiu do elemento
        let elementoHover = $(event.target);
        elementoHover.css('background-color', '#d00');
    }
);

Recommended reading: W3schools - onmouseover Event

7

If you are no longer using jQuery, you don’t need to load an entire library just for this, you can get the elements with pure Javascript:

Clicking with the mouse

document.body.onclick = function() {
  console.log(event.target);
}
<div>CLICANDO NA DIV</div>
<p>CLICANDO NO P</p>
<span>CLICANDO NO SPAN</span>
<p>CLICANDO NO P</p>

Hovering

document.body.onmouseover= function() {
  console.log(event.target);
}
<div>PASSANDO NA DIV
  <p>PASSANDO NO P</p>
  <span>PASSANDO NO SPAN</span>
  <p>PASSANDO NO P</p>
</div>

5

Use the event onmousemove in the body:

var atual = null;

function mouseEventHandler(mEvent) {
  var ultimo = atual;
  // Internet Explorer || Demais navegadores
  atual = mEvent.srcElement || mEvent.target;

  if (ultimo !== atual) {
    console.log('id do elemento atual:', atual.id);
  }
}

document.body.onmousemove = mouseEventHandler;
<div id="div1">DIV1</div>
<p id="p1">P1</p>
<span id="span1">SPAN1</span>


onmousemove

The onmousemove Property of the GlobalEventHandlers mixin is an EventHandler that processes mousemove Events.

The mousemove Event Fires when the user moves the mouse.

In free translation:

The estate onmousemove mixin GlobalEventHandlers is a EventHandler which processes the events of mousemove.

The event mousemove fires when user moves mouse.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.