I am using bootstrap and jQuery. I have 4 pictures and I only want one to show randomly each time someone hits on a button. What I have tried is to make an array of the images and then use the button to call the function but it does not work. Any idea what I am doing wrong?
This is the button
<div class="container-fluid">
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 text-center">
<button id="button" type="button" class="btn btn-primary">New Photo</button>
</div>
</div>
</div>
this is where i want the picture to display
<div class="container-fluid">
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 text-center">
<img id="picture">
</div>
</div>
</div>
This is my script
<script>
$(document).ready(function() {
$("#button").click(function() {
displayPics();
});
});
function displayPics() {
var imagesArray = new Array();
imagesArray = [
"CM.jpg", "DC.jpg", "MG.jpg", "SW.jpg"
]
var num = Math.floor(Math.random() * (imagesArray.length + 1));
var img = imagesArray[num];
$("picture").html("<img src='" + img + "' />")
}
window.onload = init;
</script>