jqueryliveready

Jquery - live ready


I need to use the function live on the document ready.

I tried this code (it does not work correctly):

$(document).live('ready', function() {

 $(".icons").contextMenu(
                { 
                    menu: 'menuIcons'                    
                }, 
                function(action, el, pos) 
                { 
                    contextMenuWork(action, el, pos); 
                }); 

function contextMenuWork(action, el, pos) {
            switch (action) {
                case "open":
                    {
                        alert("open");
                        break;
                    }
            }
            }


});

Solution

  • try this:

    function contextMenuWork(action, el, pos) {
      switch (action) {
        case "open": {
          alert("open");
          break;
        }
      }
    }
    $(".icons:not(.live)").live('click',function(e){
      if (e.which === 2) {
        e.preventDefault();
        $(this).addClass('live').contextMenu({ 
          menu: 'menuIcons'                    
        }, 
        function(action, el, pos) { 
          contextMenuWork(action, el, pos); 
        }).trigger({type:'mousedown',button:2}).trigger({type:'mouseup'});
      }
    });
    

    it uses late binding to bind to the event when the element is right clicked; it then re-triggers the right click event.