0

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

4 Answers 4

0

You should check out this link. I hope it will answer your question. Javascript multiple image change with button clicks

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

1 Comment

Thanks but I'd really appreciate it if someone could show me how to embed this or something similar into my above code
0

Here's a modern solution, note that you don't actually need to use getElementById, as long as the Id is unique across both HTML and Javascript you can just do id.innerHTMl = 'w/e';

var planets = [
  {name:"Mercury", src: "./path/to/Image.png"},
  {name:"Venus", src: "./path/to/Image.png"},
  {name:"Earth", src: "./path/to/Image.png"},
  {name:"Mars", src: "./path/to/Image.png"},
  {name:"Jupiter", src: "./path/to/Image.png"},
  {name:"Saturn", src: "./path/to/Image.png"},
  {name:"Uranus", src: "./path/to/Image.png"},
  {name:"Neptune", src: "./path/to/Image.png"}
];

var endofplanets=planets.length;
var i = -1;

btnPrev.addEventListener('click', previousplanet);
btnNext.addEventListener('click', nextplanet);

function nextplanet(){
i++;
if(i==endofplanets) i=0;

  planetName.innerHTML=planets[i].name;
  planetImage.setAttribute('src', planets[i].src);
}

function previousplanet(){
i--;
if(i<0) i=endofplanets-1;
  planetName.innerHTML=planets[i].name;
  planetImage.setAttribute('src', planets[i].src);
  
}

nextplanet();
<html>
<head>
<body bgcolor="cyan">
<h1>The Solar System</h1>


<p id="planetName"></p>

<img id="planetImage" src=""/>
  
<button id="btnPrev">Previous Planet</button>
<button id="btnNext">Next Planet</button>

</body>
</head>
</html>

Comments

0
<html>
<head>
<body bgcolor="cyan">
<h1>The Solar System</h1>
<script>
var planets=["Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune"];
var images=["mars.jpg", "venus.jpg", "earth.jpg", "mars.jpg", "jupiter.jpg", "saturn.jpg", "uranus.jpg", "neptune.jpg"];
var endofplanets=planets.length;
var i =-1;

function nextplanet(){
i=i+1;
if(i==endofplanets){
i=0;
}
document.getElementById('p1').innerHTML=planets[i];
document.getElementById('image').src=images[i];
}
</script>
<script>
function previousplanet(){
i=i-1;
if(i<0){
i=endofplanets-1;}
document.getElementById('p1').innerHTML=planets[i];

}

</script>
<img id="image">
<p id="p1">---</p>
<button type="button" onclick=nextplanet()>Next Planet</button>
<button type="button" onclick=previousplanet()>Previous Planet</button>
</body>
</head>
</html>

Be sure to actually have those images and link to those images. Also, this is VERY basic javascript, so I HIGHLY recommend that you talk to your teacher or study harder, because you'll never make it through your class by coming here for every question.

2 Comments

Thanks for the advice although I've been encouraged to come here by my teacher as this is a computer science research task
Also, I'd like to ask will the variable images has been created will it still work if I put links to images (like - nineplanets.org/images/mercury.jpg) I'd like to use instead of images saved on my PC?
0

Just a line added to your code

document.getElementById('image').src ="http://nineplanets.org/images/"+ planets[i] +".jpg"

Inside the loop, it will load the images of the planets

<html>
<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];
document.getElementById('image').src ="http://nineplanets.org/images/"+ planets[i] +".jpg"
}

function previousplanet(){

  i=i-1;
  if(i<0){
    i=endofplanets-1;}

  document.getElementById('p1').innerHTML=planets[i];
  document.getElementById('image').src ="http://nineplanets.org/images/"+ planets[i] +".jpg"

}
</script>

<img id="image">
<p id="p1">---</p>
<button type="button" onclick="nextplanet()">Next Planet</button>
<button type="button" onclick="previousplanet()">Previous Planet</button>
</body>
</html>

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.