2 Script: Element.Filters.js
3 add Filters capability to <Elements>.
11 A collection of methods to be used with <$$> elements collections.
18 Filters the collection by a specified tag name.
19 Returns a new Elements collection, while the original remains untouched.
22 filterByTag: function(tag){
23 return new Elements(this.filter(function(el){
24 return (Element.getTag(el) == tag);
29 Property: filterByClass
30 Filters the collection by a specified class name.
31 Returns a new Elements collection, while the original remains untouched.
34 filterByClass: function(className, nocash){
35 var elements = this.filter(function(el){
36 return (el.className && el.className.contains(className, ' '));
38 return (nocash) ? elements : new Elements(elements);
43 Filters the collection by a specified ID.
44 Returns a new Elements collection, while the original remains untouched.
47 filterById: function(id, nocash){
48 var elements = this.filter(function(el){
51 return (nocash) ? elements : new Elements(elements);
55 Property: filterByAttribute
56 Filters the collection by a specified attribute.
57 Returns a new Elements collection, while the original remains untouched.
60 name - the attribute name.
61 operator - optional, the attribute operator.
62 value - optional, the attribute value, only valid if the operator is specified.
65 filterByAttribute: function(name, operator, value, nocash){
66 var elements = this.filter(function(el){
67 var current = Element.getProperty(el, name);
68 if (!current) return false;
69 if (!operator) return true;
71 case '=': return (current == value);
72 case '*=': return (current.contains(value));
73 case '^=': return (current.substr(0, value.length) == value);
74 case '$=': return (current.substr(current.length - value.length) == value);
75 case '!=': return (current != value);
76 case '~=': return current.contains(value, ' ');
80 return (nocash) ? elements : new Elements(elements);