I am currently exploring the world of html and CSS, but now I am interested in delving into JavaScript, a topic I am completely unfamiliar with!
My goal is to create a random button that displays a randomly selected embedded video (for example, from a pool of 1-5 videos) within an iframe. After some research on Google, I came across a JavaScript/(jQuery?) code snippet:
<script type="text/javascript" src="jquery-1.11.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var array=["Item1", "Item2", "Item3", "Item4", "Item5"];
$('#button').bind('click', function() {
var random = array[Math.floor(Math.random() * array.length)];
$("h1").html(random);
});
});
</script>
Accompanied by the following html code:
<h1>Will be replaced</h1>
<button id="button">Random</button>
Imagine having the following iframe embedded in my html:
<iframe width="640" height="360" src="http://www.youtube.com/embed/npvNPORFXpc" frameborder="0" allowfullscreen></iframe>
My intention is to have this embedded video displayed on the page, and upon clicking the random button, it should switch to another one from the 1-5 video options. How can I incorporate this as an item in the JavaScript, ensuring each number corresponds to a unique video?
Another issue I encountered:
There are instances where the same number is generated, causing the same video to appear again. Any guidance on how to tackle this would be greatly appreciated!