Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Revisions

25 of 25
Deleted superfluous jsFiddle links and unused URLs, converted tabs into spaces, fixed comment alignment and removed some more fluff
Palec
  • 13.8k
  • 8
  • 80
  • 146

This plugin can be used in an if statement like if ($(ele).exist()) { /* DO WORK */ } or using a callback.

Plugin

;;(function($) {
 if (!$.exist) {
 $.extend({
 exist: function() {
 var ele, cbmExist, cbmNotExist;
 if (arguments.length) {
 for (x in arguments) {
 switch (typeof arguments[x]) {
 case 'function':
 if (typeof cbmExist == "undefined") cbmExist = arguments[x];
 else cbmNotExist = arguments[x];
 break;
 case 'object':
 if (arguments[x] instanceof jQuery) ele = arguments[x];
 else {
 var obj = arguments[x];
 for (y in obj) {
 if (typeof obj[y] == 'function') {
 if (typeof cbmExist == "undefined") cbmExist = obj[y];
 else cbmNotExist = obj[y];
 }
 if (typeof obj[y] == 'object' && obj[y] instanceof jQuery) ele = obj[y];
 if (typeof obj[y] == 'string') ele = $(obj[y]);
 }
 }
 break;
 case 'string':
 ele = $(arguments[x]);
 break;
 }
 }
 }
 
 if (typeof cbmExist == 'function') {
 var exist = ele.length > 0 ? true : false;
 if (exist) {
 return ele.each(function(i) { cbmExist.apply(this, [exist, ele, i]); });
 }
 else if (typeof cbmNotExist == 'function') {
 cbmNotExist.apply(ele, [exist, ele]);
 return ele;
 }
 else {
 if (ele.length <= 1) return ele.length > 0 ? true : false;
 else return ele.length;
 }
 }
 else {
 if (ele.length <= 1) return ele.length > 0 ? true : false;
 else return ele.length;
 }
 
 return false;
 }
 });
 $.fn.extend({
 exist: function() {
 var args = [$(this)];
 if (arguments.length) for (x in arguments) args.push(arguments[x]);
 return $.exist.apply(,ドル args);
 }
 });
 }
})(jQuery);

jsFiddle

You may specify one or two callbacks. The first one will fire if the element exists, the second one will fire if the element does not exist. However, if you choose to pass only one function, it will only fire when the element exists. Thus, the chain will die if the selected element does not exist. Of course, if it does exist, the first function will fire and the chain will continue.

Keep in mind that using the callback variant helps maintain chainability – the element is returned and you can continue chaining commands as with any other jQuery method!

Example Uses

if ($.exist('#eleID')) { /* DO WORK */ } // param as STRING
if ($.exist($('#eleID'))) { /* DO WORK */ } // param as jQuery OBJECT
if ($('#eleID').exist()) { /* DO WORK */ } // enduced on jQuery OBJECT
$.exist('#eleID', function() { // param is STRING && CALLBACK METHOD
 /* DO WORK */
 /* This will ONLY fire if the element EXIST */
}, function() { // param is STRING && CALLBACK METHOD
 /* DO WORK */
 /* This will ONLY fire if the element DOES NOT EXIST */
})
$('#eleID').exist(function() { // enduced on jQuery OBJECT with CALLBACK METHOD
 /* DO WORK */
 /* This will ONLY fire if the element EXIST */
})
$.exist({ // param is OBJECT containing 2 key|value pairs: element = STRING, callback = METHOD
 element: '#eleID',
 callback: function() {
 /* DO WORK */
 /* This will ONLY fire if the element EXIST */
 }
})
SpYk3HH
  • 22.6k
  • 11
  • 73
  • 81

AltStyle によって変換されたページ (->オリジナル) /