/ 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).
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
window.onload = function() { var headers = document.getElementsByTagName("h1");//grab all h1's for(i=0; i<headers.length; i++) {//loop through the h1's var elem = getNextElement(headers[i].nextSibling);//look for the next sibling of the h1 //apply these styles to the selected elements elem.style.color = "red"; elem.style.backgroundColor = "black"; } } //getNextElement function function getNextElement(node) { if(node.nodeType == 1){ return node; } if(node.nextSibling) { return getNextElement(node.nextSibling) } return null; }
Comments
