Skip to content

Instantly share code, notes, and snippets.

@denysdovhan
Created June 22, 2014 19:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save denysdovhan/cba1de26edc63f16eb77 to your computer and use it in GitHub Desktop.
Save denysdovhan/cba1de26edc63f16eb77 to your computer and use it in GitHub Desktop.
Function for search all element with class or id.
var findClass = function(className) {
var output = []; // Ініціалізуємо масив
var node = (typeof className === 'undefined' ? document.getElementsByTagName('body')[0] : document.getElementByClassName(className)).getElementsByTagName('*');
for(var i=0; i<node.length; i++) { // перебираємо всі ноди
if(node[i].getAttribute('class')) { // якщо мають атрибут class
output[i] = node[i].getAttribute('class'); // заносимо список класів в вихідний масив
}
}
return output;
};
var cells = findClass(); // запускаємо функцію
console.log(cells); // виврдимо результат роботи функції
console.log('\n');
for (var key in cells) { // перебираємо отриманий масив
var val = cells[key];
console.log(val); // виводимо список класів елементу
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment