How do you get all elements by class name using pure JavaScript? Analogous to $('.class')
in JQuery
?
-
http://stackoverflow.com/questions/1933602/how-to-getelementbyclass-instead-of-getelementbyid-with-javascript – silly Feb 24 '12 at 08:02
7 Answers
document.getElementsByClassName(klass)
Be aware that some engines (particularly the older browsers) don't have it. You might consider using a shim, if that's the case. It will be slow, and iterate over the whole document, but it will work.
EDIT several years later: You can get the same result using document.querySelectorAll('.klass')
, which doesn't seem like much, but the latter allows queries on any CSS selector, which makes it much more flexible, in case "get all elements by class name" is just a step in what you are really trying to do, and is the vanilla JS answer to jQuery's $('.class')
.

- 191,408
- 23
- 240
- 301
A Simple and an easy way
var cusid_ele = document.getElementsByClassName('custid');
for (var i = 0; i < cusid_ele.length; ++i) {
var item = cusid_ele[i];
item.innerHTML = 'this is value';
}

- 19,412
- 7
- 65
- 47
-
1When going forward, the loop will skip every second entry if elements get deleted in the loop. So, better go backwards. (I edited the answer accordingly, pending peer review.) – Bodo Thiesen Dec 08 '16 at 22:50
document.getElementsByClassName('your class');
or you can build your classname like this, if that doesn't work try this
if (!document.getElementsByClassName) {
document.getElementsByClassName=function(cn) {
var allT=document.getElementsByTagName('*'), allCN=[], i=0, a;
while(a=allT[i++]) {
a.className==cn ? allCN[allCN.length]=a : null;
}
return allCN
}
}

- 2,380
- 6
- 30
- 59
In some browsers there is a document.getElementsByClassName(class)
function. Otherwise, the only option you have is to iterate over all elements in the document by checking each of it against your condition of having the required class name.

- 9,792
- 5
- 39
- 50
Several techniques described and speed tested here: http://ejohn.org/blog/getelementsbyclassname-speed-comparison/

- 10,937
- 7
- 33
- 59
This should work:
function(className)
{
var matchingItems = [];
var allElements = document.getElementsByTagName("*");
for(var i=0; i < allElements.length; i++)
{
if(allElements [i].className == className)
{
matchingItems.push(allElements[i]);
}
}
return matchingItems;
}
-
Yes but I do not suggest to use it this way, I used try to do something like this and then I got a huge performance issue in IE7, as it running very very slow – Simon Wang Feb 24 '12 at 08:07
-
I agree. There are surely more elegant solutions. I see @Kunal below is using regular expressions which is cleaner as well. I was just providing a quick option so the requestor could get an idea. – spinon Feb 24 '12 at 08:09
-
1
function getElementsByClassName(oElm, strTagName, strClassName){
var arrElements = (strTagName == "*" && oElm.all)? oElm.all :
oElm.getElementsByTagName(strTagName);
var arrReturnElements = new Array();
strClassName = strClassName.replace(/\-/g, "\\-");
var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
var oElement;
for(var i=0; i<arrElements.length; i++){
oElement = arrElements[i];
if(oRegExp.test(oElement.className)){
arrReturnElements.push(oElement);
}
}
return (arrReturnElements)
}

- 99,167
- 15
- 158
- 162
-
This is looks similar to a code snippet in a Site Point book. Just curious if that's the source, or is it just a strange coincidence? – pqsk Mar 12 '14 at 21:31