1
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", ];

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

HTML:

<button onclick="myFunction()">Try it</button>
<img id="afbeelding">
2
  • Please state a specific question and supply a Minimal, Complete, and Verifiable Answer. See stackoverflow.com/help/mcve. Commented May 12, 2016 at 13:56
  • It doesn't show anything, can someone help me please? Commented May 12, 2016 at 13:57

2 Answers 2

2

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"];

function myFunction() {
    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.

Sign up to request clarification or add additional context in comments.

2 Comments

Great! Glad I could help. : ) Please consider accepting an answer.
Done! (I think, I'm just new here so doesn't really know how everything works :) )
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.