Skip to content Skip to sidebar Skip to footer

Jquery/javascript: Display A Random Image From An Array By Pressing A Button

var images = ['https://upload.wikimedia.org/wikipedia/commons/thumb/5/52/Playing_card_heart_5.svg/200px-Playing_card_heart_5.svg.png', 'http://www.pandasecurity.com/mediacenter/src

Solution 1:

There are many possible causes of this problem, but the most likely is that you may not have installed JQuery. In fact, you don't even need JQuery for this:

var images = ["https://upload.wikimedia.org/wikipedia/commons/thumb/5/52/Playing_card_heart_5.svg/200px-Playing_card_heart_5.svg.png", "http://www.pandasecurity.com/mediacenter/src/uploads/2014/11/short-link.jpg"];

functionmyFunction() {
    var x = Math.floor((Math.random() * images.length));
        document.getElementById('afbeelding').src = images[x];
    }

Note: Untested. Also, you had a null entry at the end of your array, so I fixed that.

Solution 2:

Look at this plunker I made with your code. By the way one ")" was missing after images.length https://plnkr.co/edit/mdyXsf5FoSPHOQGqCoO2?p=preview

var x = Math.floor((Math.random() * images.length)); $('#afbeelding').attr('src', images[x]);

Tell me if it's ok

Post a Comment for "Jquery/javascript: Display A Random Image From An Array By Pressing A Button"