Skip to content

Instantly share code, notes, and snippets.

@cnocon
Last active December 31, 2015 01:09
Show Gist options
  • Save cnocon/7911589 to your computer and use it in GitHub Desktop.
Save cnocon/7911589 to your computer and use it in GitHub Desktop.
Blog post on javascript iteration
<!DOCTYPE html>
<html>
<head>
<title>Looping in Javascript</title>
<script async src="blogpost.js"></script>
</head>
<body>
<section>
<h2>Here is our 1st section element.</h2>
</section>
<section>
<h2>Here is our 2nd section element.</h2>
</section>
<section>
<h2>Here is our 3rd section element.</h2>
</section>
</body>
</html>
window.onload= function(){
var styleSet = {
boxShadow: "0 1px 3px rgba(0,0,0,0.5)",
textShadow: "1px 1px #000",
backgroundColor: "#ffffff",
padding: "1em",
margin: "2em 0"
}
var elements = document.getElementsByTagName('section');
//console.log(elements.length)
//console.log(elements[3])
// failed with for (var e in elements) {}
for (e = 0; e < elements.length; e++) {
//console.log("hello" + elements[e])
for (var s in styleSet) {
//console.log(s)
//console.log(elements[e])
elements[e].style[s] = styleSet[s]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment