home gear home donate
Search Results For

Libraries book

HTML Library CSS Library JavaScript Library PHP Library


PHP and MySQL JavaScript HTML and CSS Vector and 3D Flash Actionscript Android Dev Miscellaneous


Website Hosting Developer Q+A
Sprite Programming Mastery
document.getElementsByClassName Method Javascript DOM

The getElementsByClassName method of the document object will return an array listing all elements that have the specified class name. You can target many elements as a group by calling this method on a class name.

NOTE: The elements you are targeting must be loaded into the document prior to calling this method on their "class" attribute. If you try to call this method on html elements that do not exist on the page(not yet loaded), or on elements that have been removed from the page, they will not be part of the array of elements returned to your script.

SYNTAX document.getElementsByClassName( class )


The value of the html "class" attribute for the group of elements that you wish to target.


<!DOCTYPE html>
<style type="text/css">
div.divGroup1 {
    background: #C0D9FE;
    padding: 12px;
    font-size: 20px;
<script type="text/javascript">
function targetGroup(className){
    var elemArray = document.getElementsByClassName(className);
    for(var i = 0; i < elemArray.length; i++){
        var elem = document.getElementById(elemArray[i].id); = "#E9EEBF";
        elem.innerHTML = "Targeted "" by class name group";
<div id="div1" class="divGroup1">Div 1</div>
<div id="div2" class="divGroup1">Div 2</div>
<div id="div3" class="divGroup1">Div 3</div>
<button onclick="targetGroup('divGroup1');">Target Class Group</button>
Div 1
Div 2
Div 3


HTML elements can also be assigned multiple class names in your document. Keep in mind that you can target those specific groups as well if you need to. The 2nd div below is the only one that is part of the "divGroup1 special" group of elements so that is the only element that will be in the returned array of elements.

<div id="div1" class="divGroup1">Div 1</div>
<div id="div2" class="divGroup1 special">Div 2</div>
<div id="div3" class="divGroup1">Div 3</div>
<button onclick="targetGroup('divGroup1 special');">
   Target Class Group
Div 1
Div 2
Div 3


Home   •   Terms of Use   •   T-Shirts   •   RSS   •   Testimonials   •   Subscribe   •   Classroom Chalkboard   •   Developer Q+A   •   Donate   •   Top ↑
Popular In PHP / MySQL E-Commerce Store Production
Social Network Website Building
Image Upload / Photo Processing
CMS Software Programming
Mass Email Systems
Magic XML Data
Search Programming
Popular In JavaScript JSON Programming
Ajax Programming
Animating Elements
WYSIWYG Programming
Date/Time Programming
DOM Scripting
Object Reference
Popular In HTML Canvas Element
New Form Elements
Audio Element
Video Element
Drag and Drop
Event Handling
Element Grouping
Popular In CSS Custom Font Embedding
Dynamic Fit Backgrounds
Theatre Mode
Box Overlays
CSS Level 1 Properties
CSS Level 2 Properties
CSS Level 3 Properties is a growing educational system packed with video and written material that you can access 24/7 100% free. The focus here in 2014 is spread between five technologies: HTML, CSS, JavaScript, PHP and MySQL. Learn programming theory, database interaction, web design, animation, graphics editing, vector art, 3D modeling, and much more. The educational experience here is a hybrid one offering both high quality video tutorials and code libraries that grow more robust every day. The code libraries are being assembled so that developers can discover all of the building blocks of their favorite languages. The libraries coupled with high quality video tutorials will take you deep down the rabbit hole very quickly. You can also ask questions and get free advice at