Project 2: Random Color Flipper in Javascript

To the continuation to project 1, I have improved to flip to new color every time you click on button.

Here is the implementation:

<html>

<body id=”flipper”>
<p id=”text” style=”font-size:50px”>Background Color: <span id=”…


This content originally appeared on DEV Community and was authored by DEV Community

To the continuation to project 1, I have improved to flip to new color every time you click on button.

Here is the implementation:

<html>

<body id="flipper">
    <p id="text" style="font-size:50px">Background Color: <span id="color"></span> </p>
    <button id="btn" style="padding: 14px 28px;" onclick="perform()">Random color!</button>
    <script>
        function perform() {
            const color = '#' + Math.random().toString(16).substring(9);
            document.getElementById('color').innerHTML = color;
            document.getElementById('flipper').style.backgroundColor = color;
        }
    </script>
</body>

</html>

Again, lets understand step by step in the code.

If you see the HTML, there is again button from which the action begins. When user clicks on Random color! button then perform action gets triggered.

In the perform function, we are capturing hexadecimal value of color using '#' + Math.random().toString(16).substring(9); like #9af6bf, #815aab, #c17546 etc.

document.getElementById('color').innerHTML = color; - setting that randomly generated color to span to show on page.
document.getElementById('flipper').style.backgroundColor = color; - Setting as background color of the whole page.

Hope things are clear.

Thanks! Happy Coding 😊.

💎 Love to see your response

  1. Like - You reached here means. I think, I deserve a like.
  2. Comment - We can learn together.
  3. Share - Makes others also find this resource useful.
  4. Subscribe / Follow - to stay up to date with my daily articles.
  5. Encourage me - You can buy me a Coffee

Let's discuss further.

  1. Just DM @urstrulyvishwak
  2. Or mention
    @urstrulyvishwak

For further updates:

Follow @urstrulyvishwak


This content originally appeared on DEV Community and was authored by DEV Community


Print Share Comment Cite Upload Translate Updates
APA

DEV Community | Sciencx (2022-02-24T15:57:38+00:00) Project 2: Random Color Flipper in Javascript. Retrieved from https://www.scien.cx/2022/02/24/project-2-random-color-flipper-in-javascript/

MLA
" » Project 2: Random Color Flipper in Javascript." DEV Community | Sciencx - Thursday February 24, 2022, https://www.scien.cx/2022/02/24/project-2-random-color-flipper-in-javascript/
HARVARD
DEV Community | Sciencx Thursday February 24, 2022 » Project 2: Random Color Flipper in Javascript., viewed ,<https://www.scien.cx/2022/02/24/project-2-random-color-flipper-in-javascript/>
VANCOUVER
DEV Community | Sciencx - » Project 2: Random Color Flipper in Javascript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/02/24/project-2-random-color-flipper-in-javascript/
CHICAGO
" » Project 2: Random Color Flipper in Javascript." DEV Community | Sciencx - Accessed . https://www.scien.cx/2022/02/24/project-2-random-color-flipper-in-javascript/
IEEE
" » Project 2: Random Color Flipper in Javascript." DEV Community | Sciencx [Online]. Available: https://www.scien.cx/2022/02/24/project-2-random-color-flipper-in-javascript/. [Accessed: ]
rf:citation
» Project 2: Random Color Flipper in Javascript | DEV Community | Sciencx | https://www.scien.cx/2022/02/24/project-2-random-color-flipper-in-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.