Skip to content

Instantly share code, notes, and snippets.

@israeljrs
Created May 12, 2015 12:56
Show Gist options
  • Save israeljrs/6b87cf68db2cb804711f to your computer and use it in GitHub Desktop.
Save israeljrs/6b87cf68db2cb804711f to your computer and use it in GitHub Desktop.
JavaScript add a Even ,odd class in your listing
<ul id="list">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
<li>Item 6</li>
</ul>
<script>
var IdRef = document.getElementById("list");
var IdChild = IdRef.getElementByTagName("li");
for (i=0; i<IdChild.length; i++) {
if(i%2 == 0) {
IdChild[i].classList.add('even');
} else {
IdChild[i].classList.add('odd');
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment