AnswerBun.com

Random Background Image from Button Click

Stack Overflow Asked by charmy on July 29, 2020

I’m trying to make a random background generator that allows the user to select a season and get a random background image. I’m using a different array and function for each season. I’m confused on what I’m doing wrong to get the background image to change. I tried using an alert in different parts of my seasonFall() and noticed that I am in-fact getting a different url for the background each time the button is clicked. So far I have only tried fall since I want to do the same thing for all other seasons.

Code:

<html>

<body>

  <header>PICK A SEASON!</header>

  <div id="contents">

    <button id="fall" onclick="seasonFall()">Fall Wallpaper</button>
    <button id="spring" onclick="seasonSpring()">Spring Wallpaper</button>
    <button id="summer" onclick="seasonSummer()">Summer Wallpaper</button>
    <button id="winter" onclick="seasonWinter()">Winter Wallpaper</button>
    
  </div>


<script>
  var winter = [
  "https://unsplash.com/photos/5AiWn2U10cw",
  "https://unsplash.com/photos/_TuI8tZHlk4",
  "https://unsplash.com/photos/_TuI8tZHlk4"
 ];

var fall = [
  "https://unsplash.com/photos/wUWP53W7KbY",
  "https://unsplash.com/photos/yuiJO6bvHi4",
  "https://unsplash.com/photos/OU5Q5_9hKvU"
];

var spring = [
  "https://unsplash.com/photos/Z8TQv3yKQd4",
  "https://unsplash.com/photos/l-rtCtc_4c0",
  "https://unsplash.com/photos/NVGKBnfrTD0"
];


var summer = [
  "https://unsplash.com/photos/m82uh_vamhg",
  "https://unsplash.com/photos/JP23z_-dA74",
  "https://unsplash.com/photos/7RQf2X6aXXI"
];

function seasonFall(){
  if (document.getElementById("fall").click){
    alert("testing!!");
    var fallselect = fall[Math.floor(Math.random() * fall.length)];
    document.body.style.backgroundImage = "url('" + fallselect + "')";
    alert("test 2!!");
    alert(fallselect);
  }
}

function seasonSpring(){
  if (document.getElementById("spring").click){
alert("spring");
  }
}

function seasonSummer(){
  if (document.getElementById("summer").click){
alert("summer");
  }
}

function seasonWinter(){
  if (document.getElementById("winter").click){
alert("winter");
  }
}


</script>

<style>

header{
  text-align:center;
  padding-top:100px;
  font-size: 20px;
}

button{
  margin-left:50px;
  margin-right:50px;

}

#contents{
    padding: 200px;
}

</style>
</body>

</html>

Links Used:

Similar Question Found:

Random background image button? (done using jQuery but I just want to use Javascript)

One Answer

fix images link then try this code

<html>

<body>

  <header>PICK A SEASON!</header>

  <div id="contents">

    <button id="fall" onclick="seasonFall()">Fall Wallpaper</button>
    <button id="spring" onclick="seasonSpring()">Spring Wallpaper</button>
    <button id="summer" onclick="seasonSummer()">Summer Wallpaper</button>
    <button id="winter" onclick="seasonWinter()">Winter Wallpaper</button>
    
  </div>


<script>







  
  
  function changeBackground(seasonArray){
    var fallselect = seasonArray[Math.floor(Math.random() * seasonArray.length)]; 
    document.body.style.backgroundImage = `url('${fallselect}')`;
  }
  
  
function seasonFall(){
  var fall = [
  "https://unsplash.com/photos/wUWP53W7KbY",
  "https://unsplash.com/photos/yuiJO6bvHi4",
  "https://unsplash.com/photos/OU5Q5_9hKvU"
];

   changeBackground(fall)
}

function seasonSpring(){
  var spring = [
  "https://unsplash.com/photos/Z8TQv3yKQd4",
  "https://unsplash.com/photos/l-rtCtc_4c0",
  "https://unsplash.com/photos/NVGKBnfrTD0"
];

    changeBackground(spring)
}

function seasonSummer(){
  var summer = [
  "https://unsplash.com/photos/m82uh_vamhg",
  "https://unsplash.com/photos/JP23z_-dA74",
  "https://unsplash.com/photos/7RQf2X6aXXI"
];
   changeBackground(summer)
}

function seasonWinter(){
    var winter = [
  "https://unsplash.com/photos/5AiWn2U10cw",
  "https://unsplash.com/photos/_TuI8tZHlk4",
  "https://unsplash.com/photos/_TuI8tZHlk4"
 ];
 changeBackground(winter)
}


</script>

<style>

header{
  text-align:center;
  padding-top:100px;
  font-size: 20px;
}

button{
  margin-left:50px;
  margin-right:50px;

}

#contents{
    padding: 200px;
}

</style>
</body>

</html>

Answered by moufed on July 29, 2020

Add your own answers!

Related Questions

Efficiently Zip multiple list in python/pandas

3  Asked on December 14, 2020 by sunni

   

Pandas Excel groupby/count

1  Asked on December 14, 2020 by nathaniel

   

How to call a php function in javascript using Ajax?

2  Asked on December 14, 2020 by mr-skan

     

Angular class names based on item values

2  Asked on December 14, 2020 by mafortis

     

is there any way to give name object?

1  Asked on December 14, 2020 by beginner-coder

 

HTML hidden not submitting

2  Asked on December 14, 2020 by connor-gaymon

     

how to find and remove array using jquery

1  Asked on December 13, 2020 by sultan-achmad

   

Laravel GraphQL and Sanctum – how to combine it?

0  Asked on December 13, 2020 by kamilon123s

     

How to save multiple figures built with a loop and mapplot packages in r?

1  Asked on December 13, 2020 by juan-carlos-rubio-polania

         

Pick from both sides?

2  Asked on December 13, 2020 by helloworld

       

how to return only the True values?

2  Asked on December 13, 2020 by hnakashima96

       

multiple image galleries on one page

2  Asked on December 13, 2020 by flw

     

System.MissingMethodException: Method not found?

34  Asked on December 13, 2020 by user603007

         

Use auto for only one variable with structured binding

3  Asked on December 13, 2020 by hkon-hgland

   

What does the & symbol mean here and whats going on here?

4  Asked on December 13, 2020 by aarat-chopra

   

How to get suffix of filename in javascript quickly?

2  Asked on December 13, 2020 by daniel-stephens

 

Ask a Question

Get help from others!

© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP