Skip to content

Instantly share code, notes, and snippets.

@mojaray2k
Created September 8, 2013 20:41
Show Gist options
  • Save mojaray2k/6488226 to your computer and use it in GitHub Desktop.
Save mojaray2k/6488226 to your computer and use it in GitHub Desktop.
The is() method is more powerful than you think. Here are a few examples:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery Is Method</title>
</head>
<body>
<div id="elem"></div>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script type="text/javascript" src="js/routes.js"></script>
<script type="text/javascript" src="is.js"></script>
</body>
</html>
// First, cache the element into a variable:
var elem = $('#elem');
// Is this a div?
elem.is('div') && console.log("it's a div");
// Does it have the bigbox class?
elem.is('.bigbox') && console.log("it has the bigbox class!");
// Is it visible? (we are hiding it in this example)
elem.is(':not(:visible)') && console.log("it is hidden!");
// Animating
elem.animate({
'width': 200
}, 1);
// is it animated?
elem.is(':animated') && console.log("it is animated!");
//Results
// it's a div
// it is hidden!
// it is animated!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment