DOM Manipulation additions

So at times I find myself needing to do some simple neighbor selection and summing in the DOM and I don’t have the tools I need so I wind up doing tedious, repetitive logic in loops, so I built a few useful utility functions:

Element.siblingsBefore() and Element.siblingsAfter() return a new Elements instance containing all the neighbors on that side of the element in respect to it’s parent in the DOM.

Element.hasStyle(name, value) tests the element for a particular value and also supports >value and Elements.excludeStyles(styles) and Elements.includeStyles(styles) filter an element set based on their styles(also supports >,< via hasStyle) Elements.mergedStyles(commaDelimitedString or array) combines multiple style values from Elements.getStyle into a single array.

Array.sumStrings() can sum the mixed string/units arrays returned by Elements.getStyle(name)

So to take this out of the abstract say I want to see how much space is taken up above me inside the element selected, but I want to exclude absolute elements and anything that’s 0 height… normally a bunch of code and tedious, but now:

Now That’s way better.

Enjoy!

Leave a Reply