Created
December 17, 2009 02:54
-
-
Save robertsosinski/258489 to your computer and use it in GitHub Desktop.
Custom jQuery Filter with Parameters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Custom jQuery Filter with Parameters</title> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> | |
<script type="text/javascript"> | |
(function($) { | |
$.expr[':'].hasTitle = function(elem, index, list) { | |
var params = eval("([" + list[2] + list[3] + list[2] + "])"); | |
for (var i = 0; i < params.length; i++) { | |
if ($(elem).attr("title") === params[i]) { | |
return true; | |
} | |
} | |
return false; | |
} | |
$(function() { | |
$("li:hasTitle('blond', 'brunette')").each(function() { | |
console.log($(this).text()); | |
}); | |
}); | |
})(jQuery); | |
</script> | |
</head> | |
<body> | |
<ul> | |
<li title="blond">Sarah</li> | |
<li title="brunette">Kim</li> | |
<li title="blond">Molly</li> | |
<li title="brunette">Michelle</li> | |
</ul> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment