2

I' m trying to use jQuery contextMenu within a joomla 3 site. The basic jquery code for the menu is:

 $(function() {
    $.contextMenu({
        selector: '.context-menu-one', 
        callback: function(key, options) {
            var m = "clicked: " + key;
            window.console && console.log(m) || alert(m); 
        },
        items: {
            "edit": {name: "Edit", icon: "edit"},
            "cut": {name: "Cut", icon: "cut"},
           copy: {name: "Copy", icon: "copy"},
            "paste": {name: "Paste", icon: "paste"},
            "delete": {name: "Delete", icon: "delete"},
            "sep1": "---------",
            "quit": {name: "Quit", icon: function(){
                return 'context-menu-icon context-menu-icon-quit';
            }}
        }
    });

I load jquery framework with JHtml::_('jquery.framework'); I replaced $function with jQuery(function(){.

How may I change $.contextMenu so that it is acceptable by Joomla? I tried many changes but I get $.contextMenu is not a function.

I also tried to use JHtml::_('jquery.framework',false); so that to use $ instead of (jQuery) but still not acceptable.

If I include jquery library from a cdn and use $ instead of (jQuery) it works fine but this way I' m getting conflicts with other extensions that use jquery.

  • jQuery contextMenu is a plugin? Are you sure you are loading the plugin and after the jQuery library and that your javascript is running after all these have been loaded in the page? – FFrewin Nov 10 '15 at 9:08
  • In fact i'm trying to use swisnl.github.io/jQuery-contextMenu/demo.html it is a jquery library. Everything is loaded. – dip Nov 10 '15 at 9:17
  • Any link to the page with the problem? – FFrewin Nov 10 '15 at 10:09
  • Try this: replace jQuery(function(){ with jQuery(function($){. – Farahmand Nov 10 '15 at 15:58
3

If you want to use the $ - sign inside your jQuery function, you have to call it like this:

jQuery(function($) {
   $.contextMenu({ ...

(Note the $ inside of function())

$ is just a shortcut often used for jQuery. In Joomla we have the problem that it often conflicts with other JavaScript libraries, like Mootools for example.

You could also call it like that:

jQuery(function() {
   jQuery.contextMenu({ ...

If you still get the error than probably the plugin is loaded before jQuery library. How do you add the plugin?

  • Yes! This was the answer. The first one worked. Thank you very much – dip Nov 10 '15 at 10:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.