Essentially, I could not find a way to do the following .click () more than once. My intention is to repeat .gen2 class clicks constantly. Delete () the current .gen2 image and replace it with a randomly selected image from myArray.
$(document).ready(function(){ $('.gen2').click(function(){ $('.gen2').remove(); var myArray = ['<img class="gen2" src="images/bottom/chinchilla.png" />', '<img class="gen2" src="images/bottom/bird.png" />', '<img class="gen2" src="images/bottom/bluejay.png" />', '<img class="gen2" src="images/bottom/walrus.png" />']; var rand = myArray[Math.floor(Math.random() * myArray.length)]; $('.change').append(rand); }); $('.gen1').click(function(){ $('.gen1').remove(); var array = ['<img class="gen1" src="images/top/raven.png" />', '<img class="gen1" src="images/top/boar.png" />', '<img class="gen1" src="images/top/trex.png" />']; var rand = array[Math.floor(Math.random() * array.length)]; $('.change').append(rand); });});
HTML is as follows. because the positions of both .gen1 and .gen2 are set to absolute, they overlap, so when each of them is deleted and replaced by .append (), a new image is formed.
<body> <div class="change"> <img class="gen1" src="images/top/raven.png" /> <img class="gen2" src="images/bottom/chinchilla.png" /> </div>
the problem is that after the first click (); the function no longer works. I canβt understand how ..
Thanks!
source share