Forum Moderators: open

Message Too Old, No Replies

Referencing a variable set in a jQuery plugin

         

csdude55

6:54 am on Apr 2, 2018 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The premise of these functions is to allow the user to click to open a box, then click anywhere else on the page to hide it.

The script is:

<script src="//code.jquery.com/jquery-2.2.4.min.js"></script>

<script>
document.onclick = function() {
if (toggle.el) {
alert('yes');
toggle.el.toggle();
}
}

$.fn.toggleClick = function(e) {
toggle.el = this;
toggle.el.toggle();

if (e.stopPropagation) e.stopPropagation();
e.cancelBubble = true;

return false;
}
</script>

// Usage
<div id="menu" style="position: absolute; display: none"></div>

<span onClick="$('#menu').toggleClick(event);
$('#menu').css({ 'right': 55, 'width': 152 });
$('#menu').ajax(link_to_script);">
Click
</span>


Now, the way I understand it, the user clicks on "Click", which then triggers $.fn.toggleClick. this will be equivalent to the selector sent (in this case, #menu), and e is the event.

So within this plugin, the variable toggle.el is now equivalent to this, so it's the same as #menu.

The plugin works correctly, toggling the display of menu and showing the AJAX, as expected.

Then, when the user clicks somewhere else on the page, it should trigger document.click. In this case, toggle.el will have already been set in the plugin, and should be equivalent to #menu, right?

I know that toggle.el has SOME value at this point, because I do get the alert "yes", as expected. But then instead of toggling the display of menu, I get an error that toggle.el.toggle is not a function.

I think that the problem is in how I'm referring to the toggle.el variable. I've tried a few variations, with mixed results:

// these didn't give an error, but didn't change anything, either
$(toggle.el).toggle();
toggle.el.style.display= 'none';

// this resulted in the same "is not a function" error
toggle.el.hide();


Any thoughts on how I need to modify this to make it work?

csdude55

1:19 am on Apr 3, 2018 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Well, I found A solution, but I don't know if it's the right, or best solution.

I was able to modify everything to use this.attr('id'), and it works:

<script>
document.onclick = function() {
if (toggle.el)
$('#' + toggle.el).toggle();
}

$.fn.toggleClick = function(e) {

// moved this here from the document.onclick function for more testing
// alerts "menu"
alert( this.attr('id') );

// alerts "undefined"
alert( this.id );
////////

if (!toggle.el)
toggle.el = this.attr('id');

this.toggle();

if (e.stopPropagation) e.stopPropagation();
e.cancelBubble = true;

return toggle.el;
}
</script>

// Usage
<div id="menu" style="position: absolute; display: none"></div>

<span onClick="toggle.el = $('#menu').toggleClick(event);
$('#menu').css({ 'right': 55, 'width': 152 });
$('#menu').ajax(link_to_script);">
Click
</span>


With this, the alert() shows "menu" so I have to manually prepend it with '#' to make the ID work. And I've read several times that this.attr('id') is more or less synonymous with this.id *, but when I use this.id or $(this).id the alert just gives me undefined.

This is a rebuild of something I built 5 or 6 years ago, and I honestly don't know why I chose toggle.el as the variable name; I don't usually make variables with a dot in the middle, so I'm concerned that it has another meaning that I've forgotten?

But either way, this works until I discover the other problems I've created from it :-P

* the only exception being that if the element has no ID then this.id will return a blank string while this.attr('id') will return undefined