SoFunction
Updated on 2025-03-03

js to select multiple or single elements (using class)


function getElementsByClassName(elem_name,elem_tags) { //elem_name: the class name of the query,elem_tags: under which element is searched
if(elem_tags == null) {
elem_tags = '*';
}
var all_elem = (elem_tags);//Return an array of elements
var arr = []; //Define an empty array to store the results
for(var i=0; i<all_elem.length; i++) { //Because all_elem is an array, the index starts from 0, so the number of traversals is 1 less than the total number of elements
if(all_elem[i].className == elem_name) {
(all_elem[i]);
}
}
return arr; //Return this result array
}