0

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 !

Jason P
  • 26,984
  • 3
  • 31
  • 45

1 Answers1

2

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.

Grim...
  • 16,518
  • 7
  • 45
  • 61
  • But if I care about the order, how can I solve this? – tahmasib Jan 12 '22 at 09:39
  • Then you're probably going to need to use cookies. Drop a cookie to say what number they've just seen, then check for that cookie when the page loads. If it doesn't exist, assume it's -1. Add 1 to that number, and show them `textToShow[cookieNumber]` . Then overwrite the cookie with the new number. – Grim... Jan 13 '22 at 10:26