querySelector method (Preliminary)

Switch View :
ScriptFree
querySelector method

[This documentation is preliminary and is subject to change.]

Retrieves the first Document Object Model (DOM) element node from descendants of the starting element node that match any selector within the supplied selector string.

Internet Explorer 8

Syntax

object.querySelector(v, )

Standards information

Parameters

v [in]

Type: BSTR

The selector string.

pel [out, retval]

Type: IHTMLElement

A DOM element node, or null if the search cannot find an element that matches the selector string.

Return value

Type: HRESULT

If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.

Type: IHTMLElement

A DOM element node, or null if the search cannot find an element that matches the selector string.

Remarks

The document search order is depth-first. This method returns the first element found. To find all matching nodes, use querySelectorAll.

The scope of the returned element node is limited to the descendants of the starting element node. If the starting element is document, the search returns nodes from the entire document.

This method does not return the starting element node. For example, div.querySelector("p div") will never return the DIV element that it starts with.

The pseudo-class selectors :hover, :focus, and :active are supported. Selectors that contain :visited or :link are ignored and no elements are returned.

You can search namespaced elements using a selector syntax based on prefix instead of the namespaceURI, for example "nsPrefix \: element", where "nsPrefix" is the prefix of a given element.

Selectors are described in detail in Understanding CSS Selectors and W3C Selectors.

Examples

This example illustrates how the selectors in the selector string are scoped to the entire document. The variable e contains the span even though the provided selector references the P element, which is outside the scope of the starting DIV element.


<html>  
<meta http-equiv="X-UA-Compatible" content="IE=8">
<!-- To limit the search to descendants of an element only, -->
<!-- call the selectors API on the specific element of interest. -->
<body>   
    <p>    
        <div id="apple">     
        <span>Some are sauce, some are pie.</span>    
        </div>   
    </p>   
<script>    
    var div = document.getElementById("apple");    
    var   e = div.querySelector("p span");    // 'e' will select the span.    
    var   f = div.querySelector("p div");     // 'f' will be null because the div is not a descendent of 'div'
</script>  
</body> 
</html>

See also

document
a
address
b
big
blockQuote
body
button
caption
center
cite
code
col
colGroup
dd
dfn
dir
div
dl
dt
em
fieldSet
form
hn
html
i
img
input type=button
input type=checkbox
input type=file
input type=image
input type=password
input type=radio
input type=reset
input type=submit
input type=text
isIndex
kbd
label
legend
li
listing
marquee
menu
noBR
ol
p
plainText
pre
s
samp
small
span
strike
strong
sub
sup
table
tBody
td
textArea
tFoot
th
tHead
tr
tt
u
ul
var
xmp
Reference
querySelectorAll
Other Resources
W3C Selectors API
W3C Selectors

 

 

Build date: 3/14/2012

Community Content

i_Cypher
Error: object doesn't support this property or method
This method generates an error "object doesn't support this property or method" in ie8 (only version I tested) when there is no DOCTYPE declared.  The only doctype I tested is &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; and everything worked as expected.  This also applies to <a href="http://msdn.microsoft.com/en-us/library/cc304115(v=vs.85).aspx">document.querySelectorAll.</a>