Category Archives: JavaScript

Simple Random Image Script

Sometimes you just need to do simple stuff on the web. Here is a simple script written in JavaScript using jQuery, that looks at <img /> tags with a class of ‘random-image’.

<img class="random-img" src="1.jpg" />

<script src=""></script>
<script type="text/javascript">


var number = Math.floor(Math.random()*3+1);
var filename = number + ".jpg";

$('img.random-img').attr('src', filename);



This script creates a random number between 1-3, this you can change depending on the amount of images you need to randomize. It then takes this number and adds the ‘jpg’ file extension to it. With the help of some jQuery, it’s easy to select the <img class=”random-image” /> and change the source attribute.

Hope you find this usefull.