Click here to Skip to main content
15,892,005 members
Articles / Web Development / HTML
Tip/Trick

Custom Menu With Right Click Using Bootstrap

Rate me:
Please Sign up or sign in to vote.
4.92/5 (8 votes)
12 Jan 2019CPOL 24.3K   323   12   7
This tip describes how to make custom right click menu.

Introduction

We can create a beautiful custom menu on right click with bootstrap panel at ease.

Let us make a div which will be right click enabled for custom menu.

HTML
<div class="col-lg-6 bg-info" id="dv_rc" style="height:400px;" >
Right Click here
</div>

To prevent default right click menu, we need to add the following code within head tag:

JavaScript
$(document).ready(function () {
$("#dv_rc").bind('contextmenu', function (e) {
e.preventDefault(); // prevents default menu 
});
});

Now, we need to make a panel ready for right click menu:

HTML
<div id="popupRC" style="display:none;" 
class="panel panel-primary  ">
        <div class="panel-heading ">Right Click Window</div>
        <div class="panel-body">
          
                <div class="form-group">
                    <label class="control-label col-md-2">Color</label>

                    <input type="color" 
                    class="form-control" id="idcolorr" />
                </div>
          
        </div>
        <div class="panel-footer"><input type="button"  
        class="btn btn-danger" value="close"  /></div>
    </div>

Please note style="display:none;". The reason behind this is not to display before right click.

Now we need to display it on right click.

We add a line of code to the previously added jquery to modify CSS to display and position right below click.

CSS
$("#dv_rc").bind('contextmenu', function (e) {
e.preventDefault(); // prevents native menu from being shown
 $("#popupRC").css({ position: "absolute", top: e.pageY, 
left: e.pageX, display: "block" });
});

Now add some code to close this window on clicking 'close' button of panel.

HTML
<input type="button" class="btn btn-danger" 
value="close" onClick="$('#popupRC').css({ display: 'none' });" />

Points of Interest

Easy way to make a right click menu using bootstrap and jquery:

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
I am a software developer .I mainly work on asp.net webform and SQLServer.But I have also done few projects using PHP , Mysql.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Karthik_Mahalingam8-Mar-17 3:54
professionalKarthik_Mahalingam8-Mar-17 3:54 
Praisegood Pin
Member 124458071-Mar-17 1:55
Member 124458071-Mar-17 1:55 
QuestionThanks Pin
Member 1302667727-Feb-17 4:54
Member 1302667727-Feb-17 4:54 
Funny how I came here after reading a Jeff Atwood article at Coding Horror. I wasn't actually interested in the topic whatsoever, but, now that I learnt how to do it (and it was so beautifully explained), I'm drowning in ideas of how it enables so many great features in web applications. Thank you for this article.

PraiseGreat Pin
CharlesDavis196226-Feb-17 21:35
CharlesDavis196226-Feb-17 21:35 
GeneralRe: Great Pin
SagSD26-Feb-17 22:04
SagSD26-Feb-17 22:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.