Usage of getNextElement Function


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

Usage of the getNextElement function. Very basic usage to pick out the h1 tags, then change the properties of the next element(not node).


Copy this code and paste it in your HTML
  1. window.onload = function() {
  2. var headers = document.getElementsByTagName("h1");//grab all h1's
  3. for(i=0; i<headers.length; i++) {//loop through the h1's
  4. var elem = getNextElement(headers[i].nextSibling);//look for the next sibling of the h1
  5.  
  6. //apply these styles to the selected elements
  7. elem.style.color = "red";
  8. elem.style.backgroundColor = "black";
  9. }
  10. }
  11. //getNextElement function
  12. function getNextElement(node) {
  13. if(node.nodeType == 1){
  14. return node;
  15. }
  16. if(node.nextSibling) {
  17. return getNextElement(node.nextSibling)
  18. }
  19. return null;
  20. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.