document.querySelectorAll gets all of the elements that match the specified CSS selector
querySelectorAll(<selector>) gets all of the elements that match the specified CSS selector. The return type is HTMLElement[].
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
</head>
<body>
<img id="apple" src="apple.png"/>
<script>
var elems = document.querySelectorAll("p, img#apple")
document.writeln(elems.length);
</script>
</body>
</html>
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
</head>
<body>
<pre id="results"></pre>
<p id="tblock">
This is a HTML document.
<span id="banana">b</span>
<span id="apple">a</span>,
<span="orange">C</span="orange">
</p>
<script>
var selElems = document.querySelectorAll("#tblock > span");
document.writeln(selElems.length);
</script>
</body>
</html>
The following code if the document object has the querySelectorAll method.
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
</head>
<body>
<p id="paratext">
This is a test.
This is a test.
This is a test.
<img src="apple.png" alt="apple"/>
</p>
<script>
var images;
if (document.querySelectorAll) {
images = document.querySelectorAll("#paratext > img");
} else {
images = document.getElementById("paratext").getElementsByTagName("img");
}
for (var i = 0; i < images.length; i++) {
images[i].style.border = "thick solid black";
images[i].style.padding = "4px";
}
</script>
</body>
</html>
Home
JavaScript Book
DOM
JavaScript Book
DOM
Document:
- The Document Object
- document.body returns the body element as HTMLBodyElement
- document.characterSet returns document character set encoding
- document.charset gets or sets the document character set encoding
- document.compatMode
- document.cookie reads and writes cookies
- document.defaultCharset gets the default character encoding
- document.defaultView
- document.getElementsByTagName( tagName )
- document.getElementsByClassName ( className )
- document.getElementsByName ( nameOfNameAttribute )
- document.images gets all the img elements and returns HTMLCollection storing all images
- document.lastModified returns the last modified time of the document
- document.location returns the URL of the current document as Location class
- document.implementation property has information about the implementation of the DOM features
- document.querySelectorAll gets all of the elements that match the specified CSS selector
- document.readyState
- document.title returns the document title, changes the document title
- document.URL property returns the URL of the current document
- document.writeln() appends content to the end of the HTML document
- Using Properties to Obtain Element Objects