Posts

jQuery Event Handler Adapter

Bridging the Gap

Older versions of jQuery are still being used (<= 1.6), and those versions do not support the new "on" method of attaching event handlers to elements. If you were authoring jQuery plugins and want to ensure compatibility when working with events, then you would want to write an adapter inside your plugin.

Categories: #JavaScript
Tags: #jQuery
Namespacing Event Handlers

Conflicting Events Handlers

Use event namespacing when attaching or detaching event listeners. This reduces the possibility of you accidentally removing event listeners that was not intended to be removed. This is especially important if you plan to use someone else's JQuery plugin that interacts with an element which you are also manipulating through your own event handler attachment or detachment. There could also be the opposite problem where someone else's JQuery plugin removes your attached listeners. This is why namespacing events is a good practice.

Categories: #JavaScript
Tags: #jQuery
Optional Options

Common Object Format

Most JQuery plugins will use the common format of customizing by providing a plain object to the plugin handler. This is well suited for plugins that provide a widget to your page where a callback might not be needed.

Categories: #JavaScript
Tags: #jQuery
JavaScript Quick Tips #3

Function Parameters from Array Values

Suppose we have a function with parameters and an array:

// A Function and an Array

function doSomething(stuff, thing1, thing2) {
  console.log("params were concatenated:" + " " + stuff + thing1 + thing2);
}

var justAnArray = ["a", "b", "c"];
Categories: #JavaScript
Tags: #jQuery
$.proxy

Getting the Right 'this'

With jQuery, you often find yourself using an event listener with a callback function like so:

// Common Event Handler

$(document).ready(function () {
  $(".my-element").click(function () {
    console.log("this is ", this);
  });
});
Categories: #JavaScript
Tags: #jQuery