Code Monkey home page Code Monkey logo

bootstrap-contextmenu's Introduction

Bootstrap Context Menu

A context menu extension of Twitter Bootstrap made for everyone's convenience.

See a [demo page] [id]. [id]:http://sydcanem.github.io/bootstrap-contextmenu/

Usage

Via data attributes

Add data-toggle="context" to any element that needs a custom context menu and via CSS set position: relative to the element.

Point data-target attribute to your custom context menu.

<div class="context" data-toggle="context" data-target="#context-menu"></div>

Via Javascript

Call the context menu via JavaScript:

$('.context').contextmenu({
  target:'#context-menu', 
  before: function(e,element) {
    // execute code before context menu if shown
  },
  onItem: function(e, element) {
    // execute on menu item selection
  }
})

target is the equivalent of the data-target attribute. It identifies the html that will be displayed.

before is a function that is called before the context menu is displayed. If this function returns false, the context menu will not be displayed. It is passed two parameters,

  • e - the original event. (You can do an e.preventDefault() to cancel the browser event)
  • element - the element where the event was set

onItem - function to be called when a menu item in contextmenu is clicked. Useful when you want to execute a specific function when an item is clicked. It is passed two parameters,

  • e - the click event.
  • element - the element of the menu item

Example

Activate and specify selector for context menu

$('#main').contextmenu({'target':'#context-menu'});

Activate within a div, but not on spans

$('#main').contextmenu({
    target: '#context-menu2',
    before: function (e, element, target) {
        e.preventDefault();
        if (e.target.tagName == 'SPAN') {
            e.preventDefault();
            this.closemenu();
            return false;
        }
        return true;
    }
});

Modify the menu dynamically

$('#main').contextmenu({
  target: "#myMenu",
  before: function(e) { 
    this.getMenu().find("li").eq(2).find('a').html("This was dynamically changed");
  }
});

Show menu name on selection

$('#main').contextmenu({
  onItem: function(e, item) {
    alert($(item).text());
  }
});

See demo.html for a complete example.

Cross-Browser Tested

I've tested the context menu plugin on IE8+, Firefox 4+ and Chrome. Let me know if you find any compatibility issues.

bootstrap-contextmenu's People

Contributors

dariusclay avatar euge avatar jhubble avatar msurguy avatar nandreasson avatar ninio avatar sydcanem avatar treet avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.