Reputation: 239
I have a page that randomly changes an image when clicked. It's probably best to have a look at this demo that works fine: http://jahyland.com/demos/ballistic/v1g/
Now I've been asked that along with changing the image when clicked on, clicking the logo will randomly select one image to randomly change. Yes, confusing.
So I have several arrays with all the various image options:
var boyhairhat = ['1.jpg','2.jpg','3.jpg','4.jpg','5.jpg','6.jpg'];
var boychin = ['1.png','2.png','3.png','4.png','5.png','6.png'];
var boymouth = ['1.png','2.png','3.png','4.png','5.png','6.png'];
etc... Then an array with all the list of arrays:
var randombit = ['boyhairhat','boychin','boymouth'];
And then the following functions to try and get the random element changed from the logo click:
function randomSelection() {
return randombit[Math.floor(Math.random() * randombit.length)];
}
$.fn.randomImage = function(selection){
$(this).click(function(){
var cur = $('#'+selection+' img').attr('src');
var rnd = cur;
while(cur==rnd){
rnd = 'selection' + selection[Math.floor(Math.random() * selection.length)];
};
$('#'+selection+' img').attr('src',rnd).show();
});
};
$('#logo').randomImage(randomSelection);
But needless to say it's not quite working. Any help greatly appreciated.
Upvotes: 1
Views: 534