Thursday, May 5, 2011

making your own selectors in jquery

It was really cool when I found out you can make your own jquery selectors.

http://james.padolsey.com/javascript/extending-jquerys-selector-capabilities/

for instance you can do this
$.extend($.expr[':'],{
inline: function(a) {
return $(a).css('display') === 'inline';
}
});

then use it like this
$(':inline'); // Selects ALL inline elements
$('a:inline'); // Selects ALL inline anchors

the article also teaches how to store data in elements and ways you can get at that data with regular expressions. This is good because sometimes I store data in made up parameters in the dom but this is not valid html but if you store key value pairs with javascript on certain elements you can store stuff in the webpage and get it using jquery selectors or select certain items with a dom flag set using the data method of jquery.

No comments: