10 examples of 'jquery get all elements with class' in JavaScript

Every line of 'jquery get all elements with 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

31function hasClass(element, clazz) {
32 return (` ${element.className} `).indexOf(` ${clazz} `) > -1;
33}
16function getElementsByClassName(className) {
17 if (document.getElementsByClassName) {
18 return document.getElementsByClassName(className);
19 } else { // IE8
20 return document.querySelectorAll('.' + className);
21 }
22}
44function bySelectorAll(selector) {
45 return document.querySelectorAll(selector);
46}
20export function getElements(selector) {
21 return [].slice.call(document.querySelectorAll(selector));
22}
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}
12export function queryElementsByClass(
13 selectors: string,
14 parentNode = document
15): any {
16 return parentNode.getElementsByClassName(selectors);
17}
14export async function getCssClasses(elementFinder: ElementFinder): Promise<string[]> {
15 const classAttr: string = await elementFinder.getAttribute('class');
16 return classAttr.split(/\s+/);
17}
27export function removeClass(elem, needle) {
28 needle = needle.trim();
29 if (elem.classList) {
30 elem.classList.remove(...needle.split(' '));
31 } else {
32 let elemClass = elem.className.trim();
33 let className = norm(elemClass);
34 needle = SPACE + needle + SPACE;
35 while (className.indexOf(needle) >= 0) {
36 className = className.replace(needle, SPACE);
37 }
38 elem.className = className.trim();
39 }
40}
78findAll (css) {
79 return Array.prototype.slice.call(this._div.querySelectorAll(css))
80}

Related snippets