10 examples of 'javascript get all elements by class' in JavaScript

Every line of 'javascript get all elements by class' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
134function getElements(className) {
135 var elements = [];
136 var el = document.getElementsByTagName('DIV');
137 var regexp=new RegExp("\\b"+className+"\\b");
138 for (var i = 0; i < el.length; i++)
139 {
140 if (regexp.test(el[i].className))
141 elements.push(el[i]);
142 }
143 return elements;
144}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

12export function queryElementsByClass(
13 selectors: string,
14 parentNode = document
15): any {
16 return parentNode.getElementsByClassName(selectors);
17}
44function bySelectorAll(selector) {
45 return document.querySelectorAll(selector);
46}
16function getElementsByClassName(className) {
17 if (document.getElementsByClassName) {
18 return document.getElementsByClassName(className);
19 } else { // IE8
20 return document.querySelectorAll('.' + className);
21 }
22}
34function forEachElementInClass(className, callback, arg) {
35 var references = document.getElementsByClassName(className);
36 for (i = 0; i < references.length; ++i) {
37 callback(references[i], i, arg);
38 }
39}
168function getElementsByClassName (element, tag, className) {
169 return filter(element.find(tag), x => angular.element(x).hasClass(className));
170}
292getElementsByClassName(className) {
293 if (typeof className !== 'string') return [];
294
295 return this.$_tree.getByClassName(className);
296}
5function $$(classname) {
6 var nodeList = document.getElementsByClassName(classname);
7 var elts = [];
8 for (var i = 0; i < nodeList.length; i++)
9 elts.push(nodeList[i]);
10 return elts;
11}
128getElementsByClassName(name) {
129 const html = this.documentElement;
130 return (html.classList.contains(name) ? [html] : [])
131 .concat(html.getElementsByClassName(name));
132}
78findAll (css) {
79 return Array.prototype.slice.call(this._div.querySelectorAll(css))
80}

Related snippets