Bootstrap ContextMenu

In this blog I’m explaining about Bootstrap contextmenu.


A contextual menu is a graphical user interface menu that appears on user interactions such as right-mouse clicks or middle-click mouse operation. This menu only offers a limited set of choices based on the application's current state. The choices available are related to the object that has been selected. 

Contextual menus are also referred to as context, shortcut or pop-up menus.

To implement custom right click context menu using Bootstrap we need to write the code as shown below.

Firstly you create html page.

Add bellow files in your page:


<script type="text/javascript" src="//"></script>
<link rel="stylesheet" type="text/css" href="//">
and the after write javascript code:
<script type="text/javascript">
    $(window).load(function () {
        (function ($, window) {
            $.fn.contextMenu = function (settings) {
                return this.each(function () {
                    $(this).on("contextmenu", function (e) {
                            .data("invokedOn", $(
                                position: "absolute",
                                left: getLeftLocation(e),
                                top: getTopLocation(e)
                            .on('click', function (e) {
                                var $invokedOn = $(this).data("invokedOn");
                                var $selectedMenu = $(;
                      , $invokedOn, $selectedMenu);
                        return false;
                function getLeftLocation(e) {
                    var mouseWidth = e.pageX;
                    var pageWidth = $(window).width();
                    var menuWidth = $(settings.menuSelector).width();
                    if (mouseWidth + menuWidth > pageWidth &&
                        menuWidth < mouseWidth) {
                        return mouseWidth - menuWidth;
                    return mouseWidth;
                function getTopLocation(e) {
                    var mouseHeight = e.pageY;
                    var pageHeight = $(window).height();
                    var menuHeight = $(settings.menuSelector).height();
                    if (mouseHeight + menuHeight > pageHeight &&
                        menuHeight < mouseHeight) {
                        return mouseHeight - menuHeight;
                    return mouseHeight;
        })(jQuery, window);
        $("#myTable td").contextMenu({
            menuSelector: "#contextMenu",
            menuSelected: function (invokedOn, selectedMenu) {
                var tr = $(invokedOn).parent("tr");
Html code:


<div style="width:80%;margin:100px auto;">
    <table id="myTable" class="table table-hover">
                <th>First Name</th>
                <th>Last Name</th>
<ul id="contextMenu" class="dropdown-menu" role="menu" style="display: none">
    <li><a tabindex="-1" href="#">Action</a></li>
    <li><a tabindex="-1" href="#">Another action</a></li>
    <li><a tabindex="-1" href="#">Something else here</a></li>
    <li class="divider"></li>
    <li><a tabindex="-1" href="#">Separated link</a></li>

 in my next post i'll explain about Convert Text Document to PDF File

Last updated:10/13/2014 8:36:57 AM
Anonymous User

Anonymous User

I am a content writter !


Leave Comment