How do you create a plugin that does not require a selector, e.g:
$.pluginName();
Out of this:
(function($)
{
$.fn.pluginName = function(options)
{
// options
};
// code
})(jQuery);
Instead of using this (to prevent other libraries clashing with the $
):
jQuery.pluginName = function(name, value, options)
{
// code
};
Since if I do this: $.pluginName()
, Firebug tells me that $.pluginName()
is not a function unless I add this: $.('a selector goes here').pluginName();
.
The empty() method removes all child nodes and content from the selected elements. Note: This method does not remove the element itself, or its attributes. Tip: To remove the elements without removing data and events, use the detach() method. Tip: To remove the elements and its data and events, use the remove() method.
The empty statement is a semicolon ( ; ) indicating that no statement will be executed, even if JavaScript syntax requires one. The opposite behavior, where you want multiple statements, but JavaScript only allows a single one, is possible using a block statement, which combines several statements into a single one.
Place it on the global jQuery instead of the prototype.
(function($)
{
//---v----------------namespacing your function in the jQuery namespace
$.pluginName = function(options)
{
// options
};
// code
})(jQuery);
Keep in mind that this
inside your function will not be a jQuery object. It will refer to the global jQuery
function.
You use
(function($)
{
$.pluginName = function(options)
{
// options
};
// code
})(jQuery);
This is an immediately executed anonymous function that takes one argument and is bound to parameter, $, to which a value, jQuery, is supplied.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With