Skip to content

Instantly share code, notes, and snippets.

@robertsosinski
Created December 17, 2009 02:54
Show Gist options
  • Save robertsosinski/258489 to your computer and use it in GitHub Desktop.
Save robertsosinski/258489 to your computer and use it in GitHub Desktop.
Custom jQuery Filter with Parameters
<!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