Languages
[Edit]
EN

JavaScript - create custom context menu

3 points
Created by:
Lani-Skinner
748

In this article, we would like to show you how to create a custom context menu using JavaScript.

Custom context menu created with JavaScript.
Custom context menu created with JavaScript.

There are two important things while creating a custom context menu:

  1. position - we want the context menu to appear next to the mouse cursor on right-click event,
  2. visibility - by default it needs to be invisible and appear only when we right-click in a specified scope.

Practical example

Edit

In this example, we create a custom context menu that appears on contextmenu event when we right-click the body element.

 

See also

Edit
  1. JavaScript - add or remove single class name from element

References

Edit
  1. Element: contextmenu event - Web APIs | MDN
  2. MouseEvent.clientX - Web APIs | MDN
  3. MouseEvent.clientY - Web APIs | MDN

Alternative titles

  1. JavaScript - create custom context-menu (with runnable example)
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join