Randomly Rotate Content Of A Span
I'm trying to change a of text to one of seven chunks of text. I've found a tutorial that can help me do it using the Javascript below, however, it relies on the selec
Solution 1:
Something like this works:
Javascript
<script type="text/javascript">
$(document).ready( function() {
// hide all of the tips
$('#tips').children('.tip').hide();
// randomly select one tip to show
var length = $("#tips .tip").length;
// **EDIT **
// This code was buggy!
//var ran = Math.floor(Math.random()*length) + 1;
//$("#tips .tip:nth-child(" + ran + ")").show();
// Use this instead:
var ran = Math.floor((Math.random()*length));
$('#tips > .tip:eq('+ran+')').show();
});
</script>
HTML
<p id="tips">
<strong>Random Tip: </strong>
<span class="tip">This is tip 1.</span>
<span class="tip">This is tip 2.</span>
<span class="tip">This is tip 3.</span>
<span class="tip">This is tip 4.</span>
<span class="tip">This is tip 5.</span>
<span class="tip">This is tip 6.</span>
<span class="tip">This is tip 7.</span>
</p>
Post a Comment for "Randomly Rotate Content Of A Span"