Skip to content

Instantly share code, notes, and snippets.

@StevenBlack
Created March 15, 2010 17:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StevenBlack/333065 to your computer and use it in GitHub Desktop.
Save StevenBlack/333065 to your computer and use it in GitHub Desktop.
:headerBelow and :headerAbove
// jQuery custom selectors for headers above (lower <h> tag) or below (higher <h> tag) given an arbitrary level
(function( $ ) {
$.extend($.expr[':'],{
// selects all the headers below the passed index. So 2 matches <h3>, <h4>, <h5>.
headerBelow: function( elem, i, match ) {
if ( /h\d/i.test( elem.nodeName ) ) {
var level= parseInt( match[ 3 ], 10 ) + 1, str = "h["+ level +"-5]", re = new RegExp( str, "i" ), ret ;
return re.test( elem.nodeName );
}
return false;
},
// selects all the headers above the passed index. So 3 matches <h1> and <h2>.
headerAbove: function( elem, i, match ) {
if ( /h\d/i.test( elem.nodeName ) ) {
var level= ( parseInt( match [ 3 ], 10 ) - 1 ) || 1, str = "h[1-"+ level +"]", re = new RegExp( str, "i" ) ;
return re.test( elem.nodeName );
}
return false;
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment