Element collector
HTML Collection on steroids.
How to use
var my_collector = ; // add elements by CSS selectormy_collector; // add specific elementmy_collector; // add elements by live HTML Collectionmy_collector; // add elements by callback function { if user_exists return document; else return document; }my_collector; // get list of all matching elementsmy_collector;
Constructor
;
Creates instance of ElementCollector
object. You can then use various methods to work with it.
Methods
add(item)
Adds new item to the list. You can add any of various types of items. Does not add duplicates if item already exists in the list.
my_collector; // CSS selectormy_collector; // elementmy_collector; // collectionmy_collector; // function
remove(item)
Removes item from the list. Does nothing if item is not in the list.
my_collector;my_collector;my_collector;my_collector;
reset()
Removes all items from the list.
get(root_node)
Returns array of all elements matching all added items. The result contains only unique elements. For example, if collector contains two CSS selectors, each returning the same elements, the result will only contain each element once.
root_node
is document.body
by default. You can use any other node, even the ones that are not added to document.body
yet (e.g. fragments). If the root_node
does not exist, returns an empty array.
Item types
- CSS selector - non-empty string
- element reference - any reference to an existing element
- elements enumerable - array (e.g.
document.getElementsByTagName
) or collection (e.g.document.querySelectorAll
) of elements - function - any function that returns single element or elements enumerable
Documentation
Bug reports, feature requests and contact
If you found any bugs, if you have feature requests or any questions, please, either file an issue at GitHub or send me an e-mail at mailto:riki@fczbkk.com.
License
Element collector is published under the MIT license.