Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / docs / api / Ext.DomQuery.html
1 <!DOCTYPE html><html><head><title>Ext.DomQuery | Ext JS 4.0 Documentation</title><script type="text/javascript" src="../ext-all.js"></script><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../scrollbars.css" type="text/css"><link rel="stylesheet" href="../docs.css" type="text/css"><link id="styleCss" rel="stylesheet" href="../style.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script><link rel="stylesheet" href="../prettify.css" type="text/css"><!-- link(rel: 'stylesheet', href: req.baseURL + '/css/ext4.css', type: 'text/css')--><link rel="shortcut icon" type="image/ico" href="../favicon.ico"><!--[if IE]>
2 <style type="text/css">.head-band { display: none; }
3 .header { border: 0; top: 0; left: 0px; background: url(../header.gif) repeat-x; }
4 .doc-tab .members .member a.more { background-color: #efefef; }
5 </style><link rel="stylesheet" href="/new/css/ie.css" type="text/css"><![endif]-->
6 </head><body id="ext-body" class="iScroll"><div id="notice" class="notice">For up to date documentation and features, visit 
7 <a href="http://docs.sencha.com/ext-js/4-0">http://docs.sencha.com/ext-js/4-0</a></div><div class="wrapper"><div class="head-band"></div><div class="header"><h2><a href="../index.html">Sencha Documentation</a></h2></div><div id="search"><form><input type="text" placeholder="Search" id="search-field" autocomplete="off" name="q"></form><div id="search-box"></div></div><div id="treePanel"></div><div id="container"><script type="text/javascript">
8
9     req = {
10         liveURL: '.',
11         standAloneMode: true,
12         origDocClass: 'Ext.DomQuery',
13         docClass: 'Ext.DomQuery',
14         docReq: 'Ext.DomQuery',
15         version: '4.0',
16         baseURL: '.',
17         baseDocURL: '.',
18         baseProdURL: '.'
19     };
20
21     clsInfo = {};
22
23
24
25 </script>
26
27 <script type="text/javascript" src="../search.js"></script>
28 <!--script type="text/javascript" src="/new/javascripts/app/examples.js"></script-->
29 <script type="text/javascript" src="../class_tree.js"></script>
30 <script type="text/javascript" src="../class_doc.js"></script>
31 <script type="text/javascript">
32     req.source = 'DomQuery.html#Ext-DomQuery';
33     clsInfo = {"methods":["compile","filter","is","jsSelect","selectNode","selectNumber","selectValue"],"cfgs":[],"properties":["matchers","operators","pseudos"],"events":[],"subclasses":[]};
34     Ext.onReady(function() {
35         Ext.create('Docs.classPanel');
36     });
37 </script><div id="top-block" class="top-block"><h1 id="clsTitle" class="cls"><a href="../source/DomQuery.html#Ext-DomQuery" target="_blank">Ext.DomQuery</a></h1></div><div id="docContent"><div id="doc-overview-content"><div class="lft"><p>Provides high performance selector/xpath processing by compiling queries into reusable functions. New pseudo classes and matchers can be plugged. It works on HTML and XML documents (if a content node is passed in).</p>
38
39 <p>
40 DomQuery supports most of the <a href="http://www.w3.org/TR/2005/WD-css3-selectors-20051215/#selectors">CSS3 selectors spec</a>, along with some custom selectors and basic XPath.</p>
41
42
43
44
45 <p>
46 All selectors, attribute filters and pseudos below can be combined infinitely in any order. For example "div.foo:nth-child(odd)[@foo=bar].bar:first" would be a perfectly valid selector. Node filters are processed in the order in which they appear, which allows you to optimize your queries for your document structure.
47 </p>
48
49
50 <h4>Element Selectors:</h4>
51
52
53 <ul class="list">
54     <li> <b>*</b> any element</li>
55     <li> <b>E</b> an element with the tag E</li>
56     <li> <b>E F</b> All descendent elements of E that have the tag F</li>
57     <li> <b>E > F</b> or <b>E/F</b> all direct children elements of E that have the tag F</li>
58     <li> <b>E + F</b> all elements with the tag F that are immediately preceded by an element with the tag E</li>
59     <li> <b>E ~ F</b> all elements with the tag F that are preceded by a sibling element with the tag E</li>
60 </ul>
61
62
63 <h4>Attribute Selectors:</h4>
64
65
66 <p>The use of &#64; and quotes are optional. For example, div[&#64;foo='bar'] is also a valid attribute selector.</p>
67
68
69 <ul class="list">
70     <li> <b>E[foo]</b> has an attribute "foo"</li>
71     <li> <b>E[foo=bar]</b> has an attribute "foo" that equals "bar"</li>
72     <li> <b>E[foo^=bar]</b> has an attribute "foo" that starts with "bar"</li>
73     <li> <b>E[foo$=bar]</b> has an attribute "foo" that ends with "bar"</li>
74     <li> <b>E[foo*=bar]</b> has an attribute "foo" that contains the substring "bar"</li>
75     <li> <b>E[foo%=2]</b> has an attribute "foo" that is evenly divisible by 2</li>
76     <li> <b>E[foo!=bar]</b> attribute "foo" does not equal "bar"</li>
77 </ul>
78
79
80 <h4>Pseudo Classes:</h4>
81
82
83 <ul class="list">
84     <li> <b>E:first-child</b> E is the first child of its parent</li>
85     <li> <b>E:last-child</b> E is the last child of its parent</li>
86     <li> <b>E:nth-child(<i>n</i>)</b> E is the <i>n</i>th child of its parent (1 based as per the spec)</li>
87     <li> <b>E:nth-child(odd)</b> E is an odd child of its parent</li>
88     <li> <b>E:nth-child(even)</b> E is an even child of its parent</li>
89     <li> <b>E:only-child</b> E is the only child of its parent</li>
90     <li> <b>E:checked</b> E is an element that is has a checked attribute that is true (e.g. a radio or checkbox) </li>
91     <li> <b>E:first</b> the first E in the resultset</li>
92     <li> <b>E:last</b> the last E in the resultset</li>
93     <li> <b>E:nth(<i>n</i>)</b> the <i>n</i>th E in the resultset (1 based)</li>
94     <li> <b>E:odd</b> shortcut for :nth-child(odd)</li>
95     <li> <b>E:even</b> shortcut for :nth-child(even)</li>
96     <li> <b>E:contains(foo)</b> E's innerHTML contains the substring "foo"</li>
97     <li> <b>E:nodeValue(foo)</b> E contains a textNode with a nodeValue that equals "foo"</li>
98     <li> <b>E:not(S)</b> an E element that does not match simple selector S</li>
99     <li> <b>E:has(S)</b> an E element that has a descendent that matches simple selector S</li>
100     <li> <b>E:next(S)</b> an E element whose next sibling matches simple selector S</li>
101     <li> <b>E:prev(S)</b> an E element whose previous sibling matches simple selector S</li>
102     <li> <b>E:any(S1|S2|S2)</b> an E element which matches any of the simple selectors S1, S2 or S3//\\</li>
103 </ul>
104
105
106 <h4>CSS Value Selectors:</h4>
107
108
109 <ul class="list">
110     <li> <b>E{display=none}</b> css value "display" that equals "none"</li>
111     <li> <b>E{display^=none}</b> css value "display" that starts with "none"</li>
112     <li> <b>E{display$=none}</b> css value "display" that ends with "none"</li>
113     <li> <b>E{display*=none}</b> css value "display" that contains the substring "none"</li>
114     <li> <b>E{display%=2}</b> css value "display" that is evenly divisible by 2</li>
115     <li> <b>E{display!=none}</b> css value "display" that does not equal "none"</li>
116 </ul>
117
118 <div class="members"><div class="m-properties"><a name="properties"></a><div class="definedBy">Defined By</div><h3 class="prp p">Properties</h3><div id="property-matchers" class="member f ni"><a href="Ext.DomQuery.html#property-matchers" rel="property-matchers" class="expand more ar"><span>&nbsp;</span></a><div class="title"><div class="meta"><a href="Ext.DomQuery.html" class="definedIn docClass">Ext.DomQuery</a><br/><a href="../source/DomQuery.html#Ext-DomQuery-property-matchers" class="viewSource">view source</a></div><a name="matchers"></a><a name="property-matchers"></a><a href="Ext.DomQuery.html#" rel="property-matchers" class="cls expand">matchers</a><span> : Object</span></div><div class="description"><div class="short">Collection of matching regular expressions and code snippets.
119 Each capture group within () will be replace the {} in ...</div><div class="long"><p>Collection of matching regular expressions and code snippets.
120 Each capture group within () will be replace the {} in the select
121 statement as specified by their index.</p>
122 </div></div></div><div id="property-operators" class="member ni"><a href="Ext.DomQuery.html#property-operators" rel="property-operators" class="expand more ar"><span>&nbsp;</span></a><div class="title"><div class="meta"><a href="Ext.DomQuery.html" class="definedIn docClass">Ext.DomQuery</a><br/><a href="../source/DomQuery.html#Ext-DomQuery-property-operators" class="viewSource">view source</a></div><a name="operators"></a><a name="property-operators"></a><a href="Ext.DomQuery.html#" rel="property-operators" class="cls expand">operators</a><span> : Object</span></div><div class="description"><div class="short">Collection of operator comparison functions. The default operators are =, !=, ^=, $=, *=, %=, |= and ~=.
123 New operator...</div><div class="long"><p>Collection of operator comparison functions. The default operators are =, !=, ^=, $=, *=, %=, |= and ~=.
124 New operators can be added as long as the match the format <i>c</i>= where <i>c</i> is any character other than space, &gt; &lt;.</p>
125 </div></div></div><div id="property-pseudos" class="member ni"><a href="Ext.DomQuery.html#property-pseudos" rel="property-pseudos" class="expand more ar"><span>&nbsp;</span></a><div class="title"><div class="meta"><a href="Ext.DomQuery.html" class="definedIn docClass">Ext.DomQuery</a><br/><a href="../source/DomQuery.html#Ext-DomQuery-property-pseudos" class="viewSource">view source</a></div><a name="pseudos"></a><a name="property-pseudos"></a><a href="Ext.DomQuery.html#" rel="property-pseudos" class="cls expand">pseudos</a><span> : Object</span></div><div class="description"><div class="short">Object hash of "pseudo class" filter functions which are used when filtering selections.
126 Each function is passed two ...</div><div class="long"><p>Object hash of "pseudo class" filter functions which are used when filtering selections.
127 Each function is passed two parameters:</p>
128
129 <ul>
130 <li><p><strong>c</strong> : Array
131   An Array of DOM elements to filter.</p></li>
132 <li><p><strong>v</strong> : String
133   The argument (if any) supplied in the selector.</p></li>
134 </ul>
135
136
137 <p>A filter function returns an Array of DOM elements which conform to the pseudo class.
138 In addition to the provided pseudo classes listed above such as <code>first-child</code> and <code>nth-child</code>,
139 developers may add additional, custom psuedo class filters to select elements according to application-specific requirements.</p>
140
141 <p>For example, to filter <code>a</code> elements to only return links to <strong>external</strong> resources:</p>
142
143 <pre><code>Ext.DomQuery.pseudos.external = function(c, v){
144     var r = [], ri = -1;
145     for(var i = 0, ci; ci = c[i]; i++){
146         // Include in result set only if it's a link to an external resource
147         if(ci.hostname != location.hostname){
148             r[++ri] = ci;
149         }
150     }
151     return r;
152 };
153 </code></pre>
154
155 <p>Then external links could be gathered with the following statement:</p>
156
157 <pre><code>var externalLinks = Ext.select("a:external");
158 </code></pre>
159 </div></div></div></div><div class="m-methods"><a name="methods"></a><div class="definedBy">Defined By</div><h3 class="mth p">Methods</h3><div id="method-compile" class="member f ni"><a href="Ext.DomQuery.html#method-compile" rel="method-compile" class="expand more ar"><span>&nbsp;</span></a><div class="title"><div class="meta"><a href="Ext.DomQuery.html" class="definedIn docClass">Ext.DomQuery</a><br/><a href="../source/DomQuery.html#Ext-DomQuery-method-compile" class="viewSource">view source</a></div><a name="compile"></a><a name="method-compile"></a><a href="Ext.DomQuery.html#" rel="method-compile" class="cls expand">compile</a>(
160 <span class="pre">String selector, [String type]</span>)
161  : Function</div><div class="description"><div class="short">Compiles a selector/xpath query into a reusable function. The returned function
162 takes one parameter "root" (optional)...</div><div class="long"><p>Compiles a selector/xpath query into a reusable function. The returned function
163 takes one parameter "root" (optional), which is the context node from where the query should start.</p>
164 <h3 class="pa">Parameters</h3><ul><li><span class="pre">selector</span> : String<div class="sub-desc"><p>The selector/xpath query</p>
165 </div></li><li><span class="pre">type</span> : String<div class="sub-desc"><p>(optional) Either "select" (the default) or "simple" for a simple selector match</p>
166 </div></li></ul><h3 class="pa">Returns</h3><ul><li><span class="pre">Function</span>&nbsp; &nbsp;
167 </li></ul></div></div></div><div id="method-filter" class="member ni"><a href="Ext.DomQuery.html#method-filter" rel="method-filter" class="expand more ar"><span>&nbsp;</span></a><div class="title"><div class="meta"><a href="Ext.DomQuery.html" class="definedIn docClass">Ext.DomQuery</a><br/><a href="../source/DomQuery.html#Ext-DomQuery-method-filter" class="viewSource">view source</a></div><a name="filter"></a><a name="method-filter"></a><a href="Ext.DomQuery.html#" rel="method-filter" class="cls expand">filter</a>(
168 <span class="pre">Array el, String selector, Boolean nonMatches</span>)
169  : Array</div><div class="description"><div class="short"><p>Filters an array of elements to only include matches of a simple selector (e.g. div.some-class or span:first-child)</p>
170 </div><div class="long"><p>Filters an array of elements to only include matches of a simple selector (e.g. div.some-class or span:first-child)</p>
171 <h3 class="pa">Parameters</h3><ul><li><span class="pre">el</span> : Array<div class="sub-desc"><p>An array of elements to filter</p>
172 </div></li><li><span class="pre">selector</span> : String<div class="sub-desc"><p>The simple selector to test</p>
173 </div></li><li><span class="pre">nonMatches</span> : Boolean<div class="sub-desc"><p>If true, it returns the elements that DON'T match
174 the selector instead of the ones that match</p>
175 </div></li></ul><h3 class="pa">Returns</h3><ul><li><span class="pre">Array</span>&nbsp; &nbsp;<p>An Array of DOM elements which match the selector. If there are
176 no matches, and empty Array is returned.</p>
177 </li></ul></div></div></div><div id="method-is" class="member ni"><a href="Ext.DomQuery.html#method-is" rel="method-is" class="expand more ar"><span>&nbsp;</span></a><div class="title"><div class="meta"><a href="Ext.DomQuery.html" class="definedIn docClass">Ext.DomQuery</a><br/><a href="../source/DomQuery.html#Ext-DomQuery-method-is" class="viewSource">view source</a></div><a name="is"></a><a name="method-is"></a><a href="Ext.DomQuery.html#" rel="method-is" class="cls expand">is</a>(
178 <span class="pre">String/HTMLElement/Array el, String selector</span>)
179  : Boolean</div><div class="description"><div class="short"><p>Returns true if the passed element(s) match the passed simple selector (e.g. div.some-class or span:first-child)</p>
180 </div><div class="long"><p>Returns true if the passed element(s) match the passed simple selector (e.g. div.some-class or span:first-child)</p>
181 <h3 class="pa">Parameters</h3><ul><li><span class="pre">el</span> : String/HTMLElement/Array<div class="sub-desc"><p>An element id, element or array of elements</p>
182 </div></li><li><span class="pre">selector</span> : String<div class="sub-desc"><p>The simple selector to test</p>
183 </div></li></ul><h3 class="pa">Returns</h3><ul><li><span class="pre">Boolean</span>&nbsp; &nbsp;
184 </li></ul></div></div></div><div id="method-jsSelect" class="member ni"><a href="Ext.DomQuery.html#method-jsSelect" rel="method-jsSelect" class="expand more ar"><span>&nbsp;</span></a><div class="title"><div class="meta"><a href="Ext.DomQuery.html" class="definedIn docClass">Ext.DomQuery</a><br/><a href="../source/DomQuery.html#Ext-DomQuery-method-jsSelect" class="viewSource">view source</a></div><a name="jsSelect"></a><a name="method-jsSelect"></a><a href="Ext.DomQuery.html#" rel="method-jsSelect" class="cls expand">jsSelect</a>(
185 <span class="pre">String selector, [Node/String root], Object type</span>)
186  : Array</div><div class="description"><div class="short"><p>Selects a group of elements.</p>
187 </div><div class="long"><p>Selects a group of elements.</p>
188 <h3 class="pa">Parameters</h3><ul><li><span class="pre">selector</span> : String<div class="sub-desc"><p>The selector/xpath query (can be a comma separated list of selectors)</p>
189 </div></li><li><span class="pre">root</span> : Node/String<div class="sub-desc"><p>(optional) The start of the query (defaults to document).</p>
190 </div></li><li><span class="pre">type</span> : Object<div class="sub-desc">
191 </div></li></ul><h3 class="pa">Returns</h3><ul><li><span class="pre">Array</span>&nbsp; &nbsp;<p>An Array of DOM elements which match the selector. If there are
192 no matches, and empty Array is returned.</p>
193 </li></ul></div></div></div><div id="method-selectNode" class="member ni"><a href="Ext.DomQuery.html#method-selectNode" rel="method-selectNode" class="expand more ar"><span>&nbsp;</span></a><div class="title"><div class="meta"><a href="Ext.DomQuery.html" class="definedIn docClass">Ext.DomQuery</a><br/><a href="../source/DomQuery.html#Ext-DomQuery-method-selectNode" class="viewSource">view source</a></div><a name="selectNode"></a><a name="method-selectNode"></a><a href="Ext.DomQuery.html#" rel="method-selectNode" class="cls expand">selectNode</a>(
194 <span class="pre">String selector, [Node root]</span>)
195  : Element</div><div class="description"><div class="short"><p>Selects a single element.</p>
196 </div><div class="long"><p>Selects a single element.</p>
197 <h3 class="pa">Parameters</h3><ul><li><span class="pre">selector</span> : String<div class="sub-desc"><p>The selector/xpath query</p>
198 </div></li><li><span class="pre">root</span> : Node<div class="sub-desc"><p>(optional) The start of the query (defaults to document).</p>
199 </div></li></ul><h3 class="pa">Returns</h3><ul><li><span class="pre">Element</span>&nbsp; &nbsp;<p>The DOM element which matched the selector.</p>
200 </li></ul></div></div></div><div id="method-selectNumber" class="member ni"><a href="Ext.DomQuery.html#method-selectNumber" rel="method-selectNumber" class="expand more ar"><span>&nbsp;</span></a><div class="title"><div class="meta"><a href="Ext.DomQuery.html" class="definedIn docClass">Ext.DomQuery</a><br/><a href="../source/DomQuery.html#Ext-DomQuery-method-selectNumber" class="viewSource">view source</a></div><a name="selectNumber"></a><a name="method-selectNumber"></a><a href="Ext.DomQuery.html#" rel="method-selectNumber" class="cls expand">selectNumber</a>(
201 <span class="pre">String selector, [Node root], Number defaultValue</span>)
202  : Number</div><div class="description"><div class="short"><p>Selects the value of a node, parsing integers and floats. Returns the defaultValue, or 0 if none is specified.</p>
203 </div><div class="long"><p>Selects the value of a node, parsing integers and floats. Returns the defaultValue, or 0 if none is specified.</p>
204 <h3 class="pa">Parameters</h3><ul><li><span class="pre">selector</span> : String<div class="sub-desc"><p>The selector/xpath query</p>
205 </div></li><li><span class="pre">root</span> : Node<div class="sub-desc"><p>(optional) The start of the query (defaults to document).</p>
206 </div></li><li><span class="pre">defaultValue</span> : Number<div class="sub-desc">
207 </div></li></ul><h3 class="pa">Returns</h3><ul><li><span class="pre">Number</span>&nbsp; &nbsp;
208 </li></ul></div></div></div><div id="method-selectValue" class="member ni"><a href="Ext.DomQuery.html#method-selectValue" rel="method-selectValue" class="expand more ar"><span>&nbsp;</span></a><div class="title"><div class="meta"><a href="Ext.DomQuery.html" class="definedIn docClass">Ext.DomQuery</a><br/><a href="../source/DomQuery.html#Ext-DomQuery-method-selectValue" class="viewSource">view source</a></div><a name="selectValue"></a><a name="method-selectValue"></a><a href="Ext.DomQuery.html#" rel="method-selectValue" class="cls expand">selectValue</a>(
209 <span class="pre">String selector, [Node root], String defaultValue</span>)
210  : String</div><div class="description"><div class="short"><p>Selects the value of a node, optionally replacing null with the defaultValue.</p>
211 </div><div class="long"><p>Selects the value of a node, optionally replacing null with the defaultValue.</p>
212 <h3 class="pa">Parameters</h3><ul><li><span class="pre">selector</span> : String<div class="sub-desc"><p>The selector/xpath query</p>
213 </div></li><li><span class="pre">root</span> : Node<div class="sub-desc"><p>(optional) The start of the query (defaults to document).</p>
214 </div></li><li><span class="pre">defaultValue</span> : String<div class="sub-desc">
215 </div></li></ul><h3 class="pa">Returns</h3><ul><li><span class="pre">String</span>&nbsp; &nbsp;
216 </li></ul></div></div></div></div></div></div></div><div id="pageContent"></div></div></div></div></body></html>