Remove jQuery Plugin Instances

Here’s how to remove all instances of all plugins from an element if the plugin does not include a destroy method:


This will not remove the bound events. You have to do that separately.
If the plugin you’re trying to remove has namespaced events, you’re in luck:

// Remove namespaced events added using .on()
// Remove namespaced events added using .bind()

If not, you can examine all of the events attached to an element:

	'$element events:', 
	$._data($element.get(0), 'events')

Here’s all wrapped up in a handy function:

var destroyCrappyPlugin = function($elem, eventNamespace) {
	var isInstantiated 	= !! $.data($elem.get(0));

	if (isInstantiated) {
		$elem.unbind('.' + eventNamespace);

Find if a Child Element Has Focus

var hasFocus = !! ($elem.find(':focus').length > 0);

Detecting child focus with focusout jQuery event, setTimeout is required to wait for next element in the DOM to receive focus:

$focusElem.on('focusout', function() {
	var $elem = $(this);
	setTimeout(function() {
		var hasFocus = !! ($elem.find(':focus').length > 0);
		if (! hasFocus) {
			// Handle blur here.
	}, 10);

Text to Links

Replace any ‘http://’ text URLs with clickable hyperlink that open in a new window:

function textToHyperlinks( $string ) {
	return preg_replace(
		'<a href="$1" target="_blank">$1</a>',

Stack Overflow