lundi 20 février 2017

change from click to page load

I am a rookie. I have tried a few different things and everything broke it. I have a page that picks a random string every time the button is clicked. I want to change it so this happens when the page is refreshed.

html

<button class="generator">Give Me Ideas!</button>
<p class="ideaBox"></p>

jquery

$(document).ready(function() {
  var currentIdea = "";

  $(".generator").click(function() {
    $(".ideaBox").html(generateIdea());
    currentIdea = $(".ideaBox").html();
  });

  function generateIdea() {
    do {
      var ideas = ["idea1",
          "idea2",
          "idea3",
          "idea4"
        ]

      var randomArrayPosition = Math.floor(Math.random() * ideas.length);
      var idea = ideas[randomArrayPosition];

    } while (idea === currentIdea)

    return idea;
  }

});

Also, is this the most efficient way to achieve this?

Thanks guys!




Aucun commentaire:

Enregistrer un commentaire