-1

Say you have an html object that when you stand on (with the mouse), a menu gets opened.

You don't have to click anything, you just move the mouse on top of that HTML element (to hover on it), then just about the end of your mouse cursor, a menu will be opened.

A mouseover possible result.

My question

If I have the element's CSS selector and I targeted this way:

document.querySelector('.myElement');

How could I emulate a mouseover?

Out of curiosity tried this:

document.querySelector('.myElement').mouseover();

but I got:

TypeError: document.querySelector(...).mouseover is not a function

I expected something like this because I always saw people using click() but not mouseover().

Maybe I should just do addEventListener();

Osi
  • 1
  • 3
  • 9
  • 30
  • 1
    Please read https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/dispatchEvent and also https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Creating_and_triggering_events – Gerardo Furtado Apr 16 '18 at 00:23
  • Only a handful of events have methods defined – [`HTMLElement` Methods](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement#Methods). – Jonathan Lonowski Apr 16 '18 at 00:24
  • 1
    Related: [Trigger onmouseover event programmatically in JavaScript](https://stackoverflow.com/a/13655046) – Jonathan Lonowski Apr 16 '18 at 00:26
  • Possible duplicate of [Trigger onmouseover event programmatically in JavaScript](https://stackoverflow.com/questions/2228376/trigger-onmouseover-event-programmatically-in-javascript) – Sebastian Simon Apr 16 '18 at 00:28
  • 1
    Why don’t you use a :hover in css instead? Very unlikely you need javascript for this. – Geuis Apr 16 '18 at 00:30
  • ^ best non-answer. This is really how it should be handled. – Kevin Peno Apr 16 '18 at 03:06

1 Answers1

-1
<div id="mydiv" onmouseover="yourFunctionOne" onmouseout="yourFunctionTwo" />

How about this?

Abhijeet Ahuja
  • 5,596
  • 5
  • 42
  • 50