styleElementSiblings Function


/ Published in: JavaScript
Save to your folder(s)

JS Function that style sibling elements of a given tag. Arguments: tag of the element We want siblings to style and name of the class we want to add. It needs the class to be styled (using CSS). It uses getNextElement function and addClass functions.

From book Dom Scripting by Jeremy Keith.


Copy this code and paste it in your HTML
  1. function styleElementSiblings(tag,theclass) {
  2. if (!document.getElementsByTagName) return false;
  3. var elems = document.getElementsByTagName(tag);
  4. var elem;
  5. for (var i=0; i<elems.length; i++) {
  6. elem = getNextElement(elems[i].nextSibling);
  7. addClass(elem,theclass);
  8. }
  9. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.