JavaScript Document own method: getElementsByClassName()
[this page | pdf | back links]
The getElementsByClassName() method (when
applied to the document object of the JavaScript
DOM) returns a
NodeList containing all the elements with the specified class attribute.
It
has the following syntax with the following parameters. It returns a
NodeList representing a collection of all relevant elements, ordered as they
appear in the source code.
document.getElementsByClassName(classname)
Parameter
|
Required / Optional
|
Description
|
classname
|
Required
|
String specifying the class
name of the elements you want to obtain. To include multiple class names,
separate individual class names by spaces
|
EXAMPLE:
HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head>
<style>
table,td,tr,th,caption {border: thin solid black; border-collapse: collapse;}
</style>
</head>
<body>
<table>
<tr><th class="classname">Dom method</th><th>Value returned</th></tr>
<tr><td>getElementsByClassName</td><td id="x3"></td></tr>
</table>
<script>
document.getElementById("x3").innerHTML =
document.getElementsByClassName("classname")[0].tagName;
</script>
</body>
</html>
|
FUNCTION THAT MAY ASSIST IN TESTING WHETHER FEATURE IS SUPPORTED:
function isSupportedJavaScriptMethodDomGetElementsByClassName() {
return !!document.getElementsByClassName;
} |
NAVIGATION LINKS
Contents | Prev | Next | JavaScript DOM (and BOM)