I have a school assignment to build a website using HTML and JavaScript about the planets in the solar system. The website is only very basic but I'm a beginner in HTML and JavaScript. The website must contain two buttons called next planet and previous planet which change the text on the screen to either the next or previous planet.
For example if the text read "Earth" and the user pressed previous planet the text would then read "Venus".
I have the above criteria working, no problem, (thanks to this website) however the next section of the assignment is for a picture of the planet to appear when the planet is on the screen for that image to change depending on what planet is being shown.
Here's the working code for the two buttons that just change the text:
<html>
<head>
<body bgcolor="cyan">
<h1>The Solar System</h1>
<script>
var planets=["Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune"];
var endofplanets=planets.length;
var i =-1;
function nextplanet(){
i=i+1;
if(i==endofplanets){
i=0;
}
document.getElementById('p1').innerHTML=planets[i]
}
</script>
<script>
function previousplanet(){
i=i-1;
if(i<0){
i=endofplanets-1;}
document.getElementById('p1').innerHTML=planets[i];
}
</script>
<p id="p1">---</p>
<button type="button" onclick=nextplanet()>Next Planet</button>
<button type="button" onclick=previousplanet()>Previous Planet</button>
</body>
</head>
</html>
If anyone knows how to add code into this that would change the image when the button is pressed also it would be greatly appriciated