jQuery Plugin: $.exist() && $.fn.exist()
by SpYk3
HTML
<div id="eleID">This element has an ID of "eleID"</div>
<div class="class-name">This element has a class name of "class-name"</div>
<div>This is just a plain div element with no ID or class name. Select this specifc one with "div:last" jQuery selector</div>
<hr />
<table>
<thead> <tr> <th> <p>Element Selector</p> </th> <th> <p>Exist ?</p> </th> </tr> </thead>
<tbody></tbody>
</table>
CSS
div { border: 1px solid blue; margin: 1em; padding: .5em 1em; text-align: center; }
table { border-collapse: separate; border-spacing: 3px 0; margin: 0 auto; }
table, th, td { border: none; }
p { padding: .2em .5em; }
th { border-bottom: 1px solid; }
th, td { border-left: 1px solid; }
th:first-child, tr td:first-child { border-left: none; border-right: 1px solid; }
JavaScript
/*---PULIGIN---*/
;;(function($){$.exist||($.extend({exist:function(){var a,c,d;if(arguments.length)for(x in arguments)switch(typeof arguments[x]){case "function":"undefined"==typeof c?c=arguments[x]:d=arguments[x];break;case "object":if(arguments[x]instanceof jQuery)a=arguments[x];else{var b=arguments[x];for(y in b)"function"==typeof b[y]&&("undefined"==typeof c?c=b[y]:d=b[y]),"object"==typeof b[y]&&b[y]instanceof jQuery&&(a=b[y]),"string"==typeof b[y]&&(a=$(b[y]))}break;case "string":a=$(arguments[x])}if("function"==typeof c){var e=0<a.length?!0:!1;if(e)return a.each(function(b){c.apply(this,[e,a,b])});if("function"==typeof d)return d.apply(a,[e,a]),a}return 1>=a.length?0<a.length?!0:!1:a.length}}),$.fn.extend({exist:function(){var a=[$(this)];if(arguments.length)for(x in arguments)a.push(arguments[x]);return $.exist.apply($,a)}}))})(jQuery);
/*---PULIGIN---*/
$("tbody").append(
$("<tr />").append( $("<td />").append($("<p />").text("#eleID")), $("<td />").append($("<p />").text($("#eleID").exist())) ),
$("<tr />").append( $("<td />").append($("<p />").text(".class-name")), $("<td />").append($("<p />").text($(".class-name").exist())) ),
$("<tr />").append( $("<td />").append($("<p />").text("div:last")), $("<td />").append($("<p />").text($("div:last").exist())) ),
$("<tr />").append( $("<td />").append($("<p />").text("#noBob")), $("<td />").append($("<p />").text($("#noBob").exist())) )
);
/*-------------OTHER CONSOLE STUFF TO LOOK AT FOR EXAMPLES--------------*/
if ($.exist('#eleID')) { console.log(1) } // param as STRING
if ($.exist($('#eleID'))) { console.log(2) } // param as jQuery OBJECT
if ($('#eleID').exist()) { console.log(3) } // enduced on jQuery OBJECT
$.exist('#eleID', function(exist) { // param is STRING && CALLBACK METHOD
console.log(4, exist)
})
$.exist($('#eleID'), function(exist) { // param is jQuery OBJECT && CALLBACK METHOD
console.log(5, exist)
})
$('#eleID').exist(function(exist) { // enduced on jQuery OBJECT...