jQuery - Easy Plugins
jQuery - Easy Plugins
Written by Ian Elliot   
Monday, 11 February 2013
Article Index
jQuery - Easy Plugins
Avoiding Collisions

Avoiding Collisions

Up to this point our plugin has been defined in the simplest way possible so that we can concentrate on how it works. In practice you would use a standard way of setting up the plugin that avoid the problem of clashes with the use of $. The problem is caused because other libraries use the $ as a shorthand symbol and so jQuery gives the user the option of selecting a different symbol. So far we have use $ in all out plugin code an this will mean if will fail if the compatibility option has been used and jQuery is using some other symbol.

The correct way to set up your code is to define a local definition of $ that is safe from changes else where. The standard way of doing is is to use an IIFE:

your plugin code using $

This defines the parameter $ to be the jQuery object so that your code can work in the usual way. Your plugin should also add itself to the $.fn object within this function which should be executed before anything attempts to use your plugin.

Using this form the complete table plugin is:


(function($) {
 $.fn.table = function(options) {
  var settings = $.extend({n: 2, m: 2}, options);
  var n = settings.n;
  var m = settings.m;

  var table = $("<table>");
  var tbody = $("<tbody>");
  var row = $("<tr>");
  var cell = $("<td>");
  for (var i = 0; i < n; i++) {
   var tempRow = row.clone();
   for (var j = 0; j < m; j++) {

   return this;

The modifications needed to make it into a plugin were very few and the process was generally easy.

Where Next

There are some advanced topics still to cover. We haven't described how to work safely with events in plugins and we haven't looked at how plugins can save data from one invocation to the next. Both are fairly easy, but are seldom necessary so look them up when you need to.

There is also the topic of how to make your plugin available to the jQuery community, and this is just a matter of setting up the project on gitHub and creating the necessary files to let people know what is in your creation. See the jQuery website for details.



Getting Started With jQuery - Advanced Ajax

So far in our exploration of jQuery we have used the "shorthand" Ajax methods. These are all implemented as calls the to the full jQuery ajax method and if you want to do anything slightly o [ ... ]

Getting Started With jQuery - Ajax Post

Now that we know all about how to get a file, it is time to post some data back to the server. In this chapter we look at both get and post as ways of sending data and see how to send form data under  [ ... ]

Other Articles

Last Updated ( Tuesday, 25 August 2015 )

Copyright © 2015 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.