⇤ home

Custom jQuery selectors

Posted 23 Nov 2009 in Javascript, Programming, and jQuery

A simple jQuery selector

jQuery is starting to be really enjoyable. This is an easy peasy way to make your own custom selector, which could be used to extract elements in a cleaner way. This also allows you to find any type of element based on one or more attributes which you couldn“t use the included selectors to find.

$.expr[':'].my_elements = function(obj){
  var $this = $(obj);
  return ($this.attr('special_attribute') != '');
};

Use the selector as usual:

$('h1:my_elements').css({color: "green"});

Related Posts