AnswerBun.com

Clicking a button and getting a different color with each click in JavaScript

Stack Overflow Asked by Zoh on January 3, 2022

How can I merge these two functions into one that changes color to a random color with each button click without reloading the page?

HTML File

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="css/style.css">
    <title>change-color</title>
</head>
<body id="body">
    <button id="button">Click Me</button>
    <button id="button-two">Click to clear</button>
    <script src="scripts/index.js"></script>
</body>
</html>

JS File

const btn = document.querySelector('#button')
const btnTwo = document.querySelector('#button-two')
const rst = document.querySelector('#reset')
const body = document.querySelector('#body')
randomNumber = Math.floor((Math.random() * 10000) - 5)



const clear = () => {
    btnTwo.addEventListener('click', (e) => {
        body.style.backgroundColor = 'white' 
        location.reload();
    }) 
    
}


const change = () => {
    btn.addEventListener('click', (e) => {
        body.style.backgroundColor = `#${randomNumber}`  
    })  
    return clear()
}

change()

One Answer

Just generate a new random number on each click instead of only on page load.

const change = () => {
    btn.addEventListener('click', (e) => {
        let randomNumber = Math.floor((Math.random() * 10000) - 5);
        body.style.backgroundColor = `#${randomNumber}`  
    })  
}

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="css/style.css">
    <title>change-color</title>
</head>
<body id="body">
    <button id="button">Click Me</button>
    <script>
const btn = document.querySelector('#button')
const btnTwo = document.querySelector('#button-two')
const rst = document.querySelector('#reset')
const body = document.querySelector('#body')


const change = () => {
    btn.addEventListener('click', (e) => {
        let randomNumber = Math.floor((Math.random() * 10000) - 5);
        body.style.backgroundColor = `#${randomNumber}`  
    })  
}

change()
    </script>
</body>
</html>

Answered by Unmitigated on January 3, 2022

Add your own answers!

Related Questions

Serialise array as single XML element in C#

1  Asked on January 25, 2021 by david-poxon

   

How to Create a Function to extract data from a JSON file

1  Asked on January 25, 2021 by ineedhelp

 

SQL create aggregated result from 2 queries

2  Asked on January 25, 2021 by sudhishkr

         

Mongoose : find document with subdocument filter

1  Asked on January 25, 2021 by temp_user

     

problems with @binding between structs

1  Asked on January 24, 2021 by mateus-neves

 

Testing Saga using Jest that have other module dependencies

0  Asked on January 24, 2021 by mzafer

   

mysql join 3 table but the “COUNT” be duplicate

1  Asked on January 24, 2021 by faisal-budiman

         

FTPS failing, but only on some commands on some machines

1  Asked on January 24, 2021 by watyf

         

Differently displayed colors on chart.js

3  Asked on January 24, 2021 by leos-literak

     

Vue Js Dynamic Class Binding Not Updating

2  Asked on January 24, 2021 by niall-parker

   

Ask a Question

Get help from others!

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