We Recommend

Pro JavaScript Techniques Pro JavaScript Techniques
Pro JavaScript Techniques is the ultimate JavaScript book for the modern web developer. It provides everything you need to know about modern JavaScript, and shows what JavaScript can do for your web sites. This book doesn't waste any time looking at things you already know, like basic syntax and structures.


Posted By

1man on 02/13/07


Tagged

js DOM getNextElement


Versions (?)


Who likes this?

2 people have marked this snippet as a favorite

shachi
vali29


Usage of getNextElement Function


Published in: JavaScript 


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

  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 

You need to login to post a comment.