Skip to content Skip to sidebar Skip to footer

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

<scripttype="text/javascript">
    $(document).ready( function() {

        // hide all of the tips
        $('#tips').children('.tip').hide();

        // randomly select one tip to showvar 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

<pid="tips"><strong>Random Tip: </strong><spanclass="tip">This is tip 1.</span><spanclass="tip">This is tip 2.</span><spanclass="tip">This is tip 3.</span><spanclass="tip">This is tip 4.</span><spanclass="tip">This is tip 5.</span><spanclass="tip">This is tip 6.</span><spanclass="tip">This is tip 7.</span></p>

Post a Comment for "Randomly Rotate Content Of A Span"