CodeIgniter Forums
jQuery Context Menu Issue - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: jQuery Context Menu Issue (/showthread.php?tid=6449)



jQuery Context Menu Issue - El Forum - 02-27-2008

[eluser]adamp1[/eluser]
I know this probably aint the best place, but th jQuery community does't seem to be has friendly as this CI one.

Anyway onto the problem. I want a very simple contextmenu to when I click an item it does something. Now this code is as basic as I can think of (just to try my point).
Code:
$('span').bind('contextmenu',function(e){
   this.text(this.text() + '.');
   return false;
});

So all I want it to do is when I right click on it, it had a . at the end. Now it does this, but it adds 4 dots? I have no idea why either. Can anyone explain?


jQuery Context Menu Issue - El Forum - 02-27-2008

[eluser]adamp1[/eluser]
One more thing, why is it sometimes using this works but then others It says it isn't valid and I have to use $(this)?


jQuery Context Menu Issue - El Forum - 02-27-2008

[eluser]Edemilson Lima[/eluser]
If it is adding 4 dots, may the functions is being called four times.

You could place a static variable into the function to act as a flag, allowing the function to add a dot only once, but I think this will work only with the first try. After the variable is set, it will not add dots anymore. To it work again, you should define another event to reset the variable.


jQuery Context Menu Issue - El Forum - 02-27-2008

[eluser]ScottBruin[/eluser]
[quote author="adamp1" date="1204177179"]One more thing, why is it sometimes using this works but then others It says it isn't valid and I have to use $(this)?[/quote]

$(this) refers to a jQuery object while 'this' refers to the DOM element--which it appears can sometime be the jQuery object. See http://remysharp.com/2007/04/12/jquerys-this-demystified/ for more.


jQuery Context Menu Issue - El Forum - 02-28-2008

[eluser]adamp1[/eluser]
Thanks for the help, the flag variable doesn't seem much of a fix more of a workaround. I have run it through firefug and as you said the code gets run 4 times. But I have no idea why. I only clicked once.


jQuery Context Menu Issue - El Forum - 02-28-2008

[eluser]xwero[/eluser]
How many spans do you have in your page?


jQuery Context Menu Issue - El Forum - 02-28-2008

[eluser]adamp1[/eluser]
9ish or more. You see its actually meant to be used for a treeview menu. So the user can right click on an item and it shows you a menu for it.


jQuery Context Menu Issue - El Forum - 02-28-2008

[eluser]adamp1[/eluser]
I think I may have found the problem. I changed it to this $('span',tree) and now it only works on spans inside my tree, it must have been getting confused with spans on the rest of the page. Cheers for pointing me in the right direction.


jQuery Context Menu Issue - El Forum - 02-28-2008

[eluser]xwero[/eluser]
i just tested it and with your original code i got an error in firebug (this.test is not a function, firebug rules Smile )
i changed it to
Code:
$('span').bind('contextmenu',function(e){
   $(this).text($(this).text() + '.');
   return false;
});
And i only could add a one dot per right click. The same happened replacing text with html.
Code:
$('span').bind('contextmenu',function(e){
   $(this).replaceWith($(this).text() + '.');
   return false;
});
Seems to work only once, the additional right clicks show the default context menu.

Hope this gets you somewhere