Tree Traversal Flashcards
Get single element children
var children = element.children();
var children = element.children;
Get single element children filtered by selector
var children = element.children(‘div’);
var children = element.queryAll(‘> div’);
var children = elements.children();
ES6
var children = elements.map(x => […x.children]); children = [].concat(…children);
ES5
var children = elements.reduce(function(result, element) { return result.concat([].slice.call(element.children)); }, []);
Get children filtered by selector from a set of elements
var children = elements.children(‘div’);
ES6
var children = elements.map(x => x.queryAll(‘> div’)); children = [].concat(…children);
ES5
var children = elements.reduce(function(result, element) { return result.concat(element.queryAll(‘> div’)); }, []);
Get single elements contents
var contents = element.contents();
var contents = element.childNodes;
Get multiple elements contents
var contents = elements.contents();
ES6
var contents = elements.map(x => […x.childNodes]); contents = [].concat(…contents);
ES5
var contents = elements.reduce(function(result, element) { return result.concat([].slice.call(element.childNodes)); }, []);
Get closing matching parent for a single element
var closest = element.closest(‘div’);
var closest = element.closest(‘div’);
Get closest matching parent for a set of elements
var closest = elements.closest(‘div’);
ES6
var closest = elements.map(x => x.closest(‘div’));
ES5
var closest = elements.map(function(element) { return element.closest(‘div’); });
Find descendants of a single element
var found = element.find(‘div’);
var found = element.queryAll(‘div’);
Find descendants in the set of elements
var found = elements.find(‘div’);
ES6
var found = elements.map(x => x.queryAll(‘div’)); found = [].concat(…found);
ES5
var found = elements.reduce(function(result, element) { return result.concat(element.queryAll(‘div’)); }, []);
Get the next sibling for a single element
var next = element.next();
var next = element.nextElementSibling;
Get the next sibling filtered by selector for a single element
var next = element.next(‘div’);
var next = element.query(‘+ div’)
Get the next sibling for each element in the set
var next = elements.next();
ES6
var next = elements .map(x => x.nextElementSibling) .filter(x => x);
ES5
var next = []; elements.forEach(function(element) { if (element.nextElementSibling) next.push(element.nextElementSibling); });
Get the next sibling filtered by selector for each element in the set
var next = elements.next(‘div’);
ES6
var next = elements .map(x => x.query(‘+ div’)) .filter(x => x);
ES5
var next = []; elements.forEach(function(element) { if (element.nextElementSibling && element.nextElementSibling.matches(‘div’)) next.push(element.nextElementSibling); });
Get all the following sibling for an element
var next = element.nextAll();
var next = element.queryAll(‘~ *’);