An undocumented selector for jQuery – Contains Word

Here’s a useful and as yet undocumented feature of jQuery’s attribute selector, the contains word feature – here’s how it works:

suppose you want to select a series of elements based on a class or rel or whatever. In this case I want to select all the links with the class .mylinks – but only those with the rel containing ‘something’:

<a rel="something else" href="#">this is a link</a>
<a rel="thething other" href="#">this is a link</a>
<a rel="something more" href="#">this is a link</a>
<a rel="something up" href="#">this is a link</a>
<a rel="nothing down" href="#">this is a link</a>
<script type="text/javascript" charset="utf-8">
$(function() {
//contains word
$(‘.mylinks[rel~=something]‘).addClass(‘foundyou’); //note the important selector: ~=
});
</script>

You can follow any responses to this entry through the RSS 2.0 feed.