How to get all elements by class name? [duplicate]
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')
.
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';
}
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
}
}