vendredi 6 novembre 2015

random div on page load

I am trying to randomize the hero content of a home page. I have this simple code, but it affects all the divs on the page, and I only want it to affect a few.

var elems = $("div");
if (elems.length) {
var keep = Math.floor(Math.random() * elems.length);
for (var i = 0; i < elems.length; ++i) {
if (i !== keep) {
  $(elems[i]).hide();
}
}
}

Here is my html:

<div id="hero1">One</div>

<div id="hero2">Two</div>

<div id="hero3">Three</div>

<div id="constant">This content does not rotate.</div>

There is another caveat to this, I need it to work within a crappy CMS that strips out my class tags. So it has to be a solution that identifies the divs based on id.




Aucun commentaire:

Enregistrer un commentaire