/* Testing .is() and .not() for jQuery collections and raw elements */
var $test1 = $('#test1');
var $test2 = $('#test2');
var $test3 = $('#test3');
var $test4 = $('#test4');
var $test = $('div.test');
var $filter = $('div.filter');
var $closest = $('#closest');
var $test5 = $('#test5');
/** Adds the result of the test to the results div */
function addPass( testName ) {
$('<div class="pass">'+ testName + ' passed.</div>').appendTo('#results');
}
function addFail( testName ) {
$('<div class="fail">'+ testName + ' failed.</div>').appendTo('#results');
}
/** Does a test, displays result */
function doTest( name, test ) {
try {
if ( test() ) {
addPass( name );
} else {
addFail( name );
}
} catch (e) {
addFail( name );
if (console) { console.error(name, ' - exception caught: ', e); }
}
}
/** Testing objects and elements for is, then passing it off to is */
$.fn.newIs = function ( element ) {
var ret = false;
if (typeof element === "object") {
var els = $(element).get();
this.each(function( i, item ) {
if (els.indexOf(item) > -1) ret = true;
});
} else {
ret = this.is( element );
}
return ret;
};
doTest('.not() with jQuery object', function () { return !$test.not( $test3 ).find('#test3').length; });
doTest('.not() with raw element', function () { return !$test.not( $test4[0] ).find('#test4').length; });
doTest('.filter() with jQuery object', function () { return $test.filter( $filter ).length === 2; });
doTest('.filter() with raw element', function () { return $test.filter( $filter[0] ).length === 1; });
doTest('.is() with jQuery object', function () { return $test1.is( $test ); });
doTest('.is() with raw element', function () { return $test1.is( $test[0] ); });
doTest('.closest() with jQuery object', function () { return $test5.closest($closest).length; });
doTest('.closest() with raw element', function () { return...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.