Errorsum
Errorsum

Reputation: 93

Jquery click event not firing more than once

I'm having a problem wherein my JQuery click event only fires once. Basically, I have a small page set up with a button that reads Generate Buzzword. When you click on the button, the div "Oren-is" empties, and another div fades out. Then, a string from an array is appended to the emptied div. All of this works correctly with the code right now, but it only happens once.

I'm sure this is a very simple question: basically, how do I get this to happen more than once? After I click on "generate buzzword" the first time, everything fires correctly. However, I can't click it more than once!

Here is my code:

var $buzzwords = ["Oren Weingrod is object-oriented.", "Oren Weingrod is agile.", "Oren Weingrod is a human.", "Oren Weingrod is analytics driven.", "Oren Weingrod is dynamic!", "Oren Weingrod is lean", "Oren Weingrod is tele-commuting", "Oren Weingrod is integration testing"];

var $buzz = $('.generate-buzzword');
$(document).ready(function(){
var random = Math.floor((Math.random(8))*10);
alert(random);
$buzz.on("click", function(){
    $('#smalltext').remove();
    $('#oren-is').empty();
    $('#oren-is').append($buzzwords[random]);

    });
});

Relevant HTML:

<section id="oren">
            <p id="oren-is">Oren* Weingrod is not a pine tree. </p> 
            <a href="#" class="generate-buzzword"> Generate Buzzword </a> 
            <p id="smalltext">*Oren means pine tree in Hebrew.</p>
</section>

Upvotes: 0

Views: 737

Answers (1)

veddermatic
veddermatic

Reputation: 1082

To go along with my comment, change your code to actually generate a new radom number on each click:

$buzz.on("click", function(){
  var random = Math.floor((Math.random($buzzwords.length))*10);
  $('#smalltext').remove();
  $('#oren-is').empty();
  $('#oren-is').append($buzzwords[random]);

  });
});

I also made it so your magic number of '8' is now the length of your array of buzzwords, so when you add / remove, you don't have to update your code =)

Upvotes: 2

Related Questions