Jump to content

Passing Variables From Function to Function


Adamhumbug
Go to solution Solved by requinix,

Recommended Posts

Hi All,

I have been reading about scope and how variables are scoped to functions etc...

I have the following script that creates a context menu and i am trying to set "linkText" to = linkText set in the click function.

$('.custom-lc').click(function() {
  $link = $(this).data('link')
  linkText = $(this).data('link')
})

var contextMenu = $('.custom-lc').contextMenu();
contextMenu.button = mouseButton.LEFT;

contextMenu.menu().addItem("linkTextVarToGoHere", function() {
  contextMenu.onclick = function() {
    window.location.href = $link
  }
});
contextMenu.init();

The issue that i have is that if i put all of the rest of the code inside the click function, it generates another contect menu for each click rather than modifying the first one.

The code that i have posted here works just fine apart from i am not able to set the text value in the addItem function.

Any pointers on the best way to pass a variable into this function would be appreciated as i have managed to pass the $link into the window.location.href and that works.

This is the context menu that i am using for reference - https://www.jqueryscript.net/menu/context-menu-material.html

Edited by Adamhumbug
Link to comment
Share on other sites

  • Solution

You've skipped past one very particular problem: you want an existing menu item's text to reflect something that can change. Using a variable for the addItem's label doesn't mean that you can change the variable's value later and the label will update. You would have to add and remove these items every time the custom-lc is clicked.

So the truth is actually that no, the code you've posted doesn't work. Not that you're using it wrong but that it can't do what you want. And I don't see anything in the library you're using that lets you edit menu labels.

I'm not sure how you came across that library, but when I search for "jQuery context menu" the very first result is this one, which looks like a much better option given that it's been updated during this year and, you know, it has actual documentation.

Link to comment
Share on other sites

44 minutes ago, requinix said:

You've skipped past one very particular problem: you want an existing menu item's text to reflect something that can change. Using a variable for the addItem's label doesn't mean that you can change the variable's value later and the label will update. You would have to add and remove these items every time the custom-lc is clicked.

So the truth is actually that no, the code you've posted doesn't work. Not that you're using it wrong but that it can't do what you want. And I don't see anything in the library you're using that lets you edit menu labels.

I'm not sure how you came across that library, but when I search for "jQuery context menu" the very first result is this one, which looks like a much better option given that it's been updated during this year and, you know, it has actual documentation.

It is updating the link buttons value which led me to think that i would be able to get somewhere with it.  Think you might be right though - this does look like a better option.

Thanks Again

Link to comment
Share on other sites

It might be me - it is likely me - but i cannot see anything about how to dynamically populate the items in the list.

I think it is my lack of understanding of their terminology.  Ill keep looking but if anyone has used this or similar before and has any pointers that would be really handy.

Link to comment
Share on other sites

7 hours ago, Adamhumbug said:

It might be me - it is likely me - but i cannot see anything about how to dynamically populate the items in the list.

If you're talk about the new one linked by Requinix, you'd use the build configuration option.  You provide a function, the script calls that function when it's time to display the menu.  You can dynamically build your items in the function and return them in the items configuration option.

 

Link to comment
Share on other sites

2 hours ago, kicken said:

If you're talk about the new one linked by Requinix, you'd use the build configuration option.  You provide a function, the script calls that function when it's time to display the menu.  You can dynamically build your items in the function and return them in the items configuration option.

 

Ok. Thanks for that. I’ll give it a go. 

Link to comment
Share on other sites

I got this far:

$(function() {
  $.contextMenu({
    selector: '.context-menu-one',
    build: function($triggerElement, e){
      return {
        callback: function(){},
        items: {
          menuItem: {name: "My on demand menu item"}
        }
      };
    }
  });
});

The above works

$(function() {
  $.contextMenu({
    selector: '.context-menu-one',
    build: function($triggerElement, e){
      return {
        callback: function(){
          $el = "SOMETHING"
        },
        items: {
          menuItem: {name: $el}
        }
      };
    }
  });
});

The above doesnt.

I am assuming that i am making the function wrong.

Edited by Adamhumbug
Link to comment
Share on other sites

Hold the phone i think i got it.

$(function() {
  $.contextMenu({
    selector: '.context-menu-one',
    build: function($triggerElement, e){
      $el = $triggerElement.data('stuff')
      return {
        callback: function(){

        },
        items: {
          menuItem: {name: $el}
        }
      };
    }
  });
});

Thanks you all so much for your feedback here and puttiung up with my rambling.

Edited by Adamhumbug
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.