getElementsByClassName method
9 | 3 | |||
Returns a NodeList collection that contains all descendant elements of the current element with the specified class name.
Note: The getElementsByClassName method is supported in Firefox from version 3 and Internet Explorer from version 9.
The returned NodeList collection is a live collection, which means that the modification of the document affects the collection. See Example 2 for details.
If you need a more complex search, use the querySelector and querySelectorAll methods. They search for elements based on selector matching.Syntax:
You can find the related objects in the Supported by objects section below.
Parameters:
Required. String that specifies the space-delimited list of class names to look for. This parameter is case-sensitive. |
Return value:
Returns a NodeList collection filled with the matching elements. The elements in the returned collection are in source order.
Example HTML code 1:
This example illustrates the use of the getElementsByClassName method:
|
||||
<head> <style> .redText { color:#FF0000; } .boldText { font-weight:bold; } .italicText { font-style:italic; } </style> <script type="text/javascript"> function GetRedTextElements () { var container = document.getElementById ("container"); if (container.getElementsByClassName) { var redTags = container.getElementsByClassName ("redText"); alert ("There are " + redTags.length + " elements in the container with class of 'redText'."); for (var i = 0; i < redTags.length; i++) { var redTag = redTags[i]; alert ("The contents of the " + (i + 1) + ". redText element are\n" + redTag.innerHTML); } } else { alert ("Your browser does not support the getElementsByClassName method."); } } </script> </head> <body> <button onclick="GetRedTextElements ()">Get the elements in the container with class of 'redText'!</button> <br /><br /> <div id="container"> <span class="redText boldText">A bold element in the container with class of 'redText'.</span> <br /> <span class="redText italicText">An italic element in the container with class of 'redText'.</span> </div> <br /><br /> <span class="redText boldText">Another element outside the container with class of 'redText'.</span> </body> |
||||
|
||||
Did you find this example helpful?
|
Example HTML code 2:
This example illustrates the meaning of live collections with the getElementsByClassName method:
|
||||
<head> <style> .redText { color:#FF0000; } </style> <script type="text/javascript"> function GetRedTextElements () { var container = document.getElementById ("container"); if (container.getElementsByClassName) { var redTags = container.getElementsByClassName ("redText"); alert ("There are " + redTags.length + " elements in the container with class 'redText'."); // add a new bold element to the container var newRedText = document.createElement ("b"); newRedText.innerHTML = "new redText element"; newRedText.className = "redText"; container.appendChild (newRedText); alert ("An element with class 'redText' has been added to the container."); alert ("There are " + redTags.length + " elements in the container with class 'redText'."); // remove a bold element from the container container.removeChild (newRedText); alert ("The previously added element has been removed from the container."); alert ("There are " + redTags.length + " elements in the container with class 'redText'."); } else { alert ("Your browser does not support the getElementsByClassName method."); } } </script> </head> <body> <button onclick="GetRedTextElements ()">Get the elements in the container with 'redText' class!</button> <br /><br /> <div id="container"> <b class="redText">A bold element in the container with 'redText' class.</b> <br /> <i class="redText">An italic element in the container with 'redText' class.</i> <br /> </div> </body> |
||||
|
||||
Did you find this example helpful?
|
Supported by objects:
document, XMLDocument
HTML elements:
a, abbr, acronym, address, b, bdo, big, blink, blockquote, body, button, caption, center, cite, code, col, colgroup, dd, del, dfn, dir, div, dl, dt, em, fieldset, font, form, frameset, h1, h2, h3, h4, h5, h6, head, html, i, img, ins, kbd, keygen, label, legend, li, listing, map, marquee, menu, nobr, noframes, noscript, object, ol, optgroup, option, p, plaintext, pre, q, rt, ruby, s, samp, select, small, span, strike, strong, sub, sup, table, tbody, td, textarea, tfoot, th, thead, tr, tt, u, ul, var, xml, xmp
Related pages:
querySelector
querySelectorAll
getElementById
getElementsByName
getElementsByTagName
getElementsByTagNameNS
querySelectorAll
getElementById
getElementsByName
getElementsByTagName
getElementsByTagNameNS
External links:
User Contributed Comments