anavdesign
4/22/2016 - 3:33 AM

JS: Is Empty

JS: Is Empty

/*
 * Check if an Element Is Empty
 * http://stackoverflow.com/questions/6813227/how-do-i-check-if-an-html-element-is-empty-using-jquery
 * https://css-tricks.com/snippets/jquery/check-if-element-exists/
 * http://stackoverflow.com/questions/14535733/how-to-check-if-div-element-is-empty
 */

/***********************************************************
 ***********************************************************
 
Usage
$('elm').isEmpty(function() {
  this.append('<p>I am empty!</p>');
});

 ***********************************************************
 ***********************************************************/

(function ($) {

	$.fn.isEmpty = function(callback) {
		var args = [].slice.call(arguments, 1);

		if( !$.trim( $(this).html() ).length) {
			callback.call(this, args);
		}

		return this;
	};
 
}(jQuery));