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

neogeek on 07/01/06


Tagged

css style strip elements


Versions (?)


Who likes this?

10 people have marked this snippet as a favorite

luman
arcturus
luxuryluke
splorp
jonbaer
soxiam
trickychicken
buscarini
SpinZ
esquareda


Stripe Elements


Published in: JavaScript 


This script will allow you to alternately place a style on objects (in this example, every other list item).

  1. function stripeElement(objects, className) {
  2.  
  3. for (var i = 0; i < objects.length; i++) {
  4. if (i % 2) { objects[i].className += ' ' + className; }
  5. }
  6.  
  7. }
  8.  
  9. window.onload = function() {
  10.  
  11. if (document.getElementById('list')) {
  12. js_lib.css.mod.stripeElement(document.getElementById('list').getElementsByTagName('li'), 'tint');
  13. }
  14.  
  15. }

Report this snippet 

Comments

RSS Icon Subscribe to comments
Posted By: Roshambo on July 27, 2006

Your for loop could probably be better written as:

for(var i = 0; i < objects.length; i = i + 2) {
    objects[i].className += ' ' + className;
}

That way you wouldn't have to nest an "if" condition in the loop.

You need to login to post a comment.