Search code examples
jquerypageloaddynamic-contentdynamic-text

Changing text on each page load


I'm just starting with jQuery/Javascript and I'm still kind of newbie.

I have a list of phrases and I need them to change inside a <h4> everytime the page loads/reloads.

I think this is a basic question, but I can't get the solution (my mind says it's very easy, but my current coding abilities don't).

Thanks in advance !


Solution

  • If you don't care about the order, try:

    var textToShow = ['text1', 'text2', 'text3', 'text4']
    $(document).ready(function() {
        $("h4").html(textToShow[Math.floor(Math.random()*textToShow.length)]);
    });
    

    Here is a fiddle: http://jsfiddle.net/JZDnv/ Keep clicking 'run' to get different things.