Skip to content

Instantly share code, notes, and snippets.

@itarato
Created September 15, 2016 11:46
Show Gist options
  • Save itarato/65c5c4314426bc1ce2e91af5f332673a to your computer and use it in GitHub Desktop.
Save itarato/65c5c4314426bc1ce2e91af5f332673a to your computer and use it in GitHub Desktop.
DOM subtree class toggling debugger
(function () {
'use strict';
console.log('Debug has started.');
var $base = jQuery('<SELECTOR>');
var list = traverse($base[0]);
var classes = [];
var element = null;
jQuery(toggleClass);
function traverse(node) {
console.log("Start traversing for children.");
var list = [];
var stack = [node];
while (stack.length) {
var currentNode = stack.pop();
var $children = jQuery(currentNode).children();
list.push(currentNode);
for (var i = 0; i < $children.length; i++) {
stack.push($children[i]);
}
}
console.log("Found ", list.length, " child nodes.");
return list;
}
function toggleClass() {
if (classes.length == 0) {
if (list.length == 0) return;
element = list.pop();
classes = (jQuery(element).attr('class') + '').replace(/\s+/g, ' ').trim().split(' ');
}
var currentClass = classes.pop();
jQuery(element).removeClass(currentClass);
setTimeout(function () {
if (!confirm(currentClass)) {
return;
}
jQuery(element).addClass(currentClass);
toggleClass();
}, 100);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment