Skip to content

Latest commit

 

History

5 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Interactive Color Changer

This is a simple web application that allows users to change the background color of a box dynamically by clicking a button. The project demonstrates basic HTML, CSS, and JavaScript integration.

Features

  • Random Color Generation: Each click generates a new random color.
  • Responsive Design: Works on all screen sizes.
  • User-Friendly Interface: Simple and intuitive design.

Technologies Used

  • HTML: For structuring the webpage.
  • CSS: For styling the elements.
  • JavaScript: For adding interactivity.

How It Works

HTML Structure

The HTML file (index.html) contains the following key elements:

  • A div with the ID color-box that represents the box whose color will change.
  • A button with the ID change-color-btn that triggers the color change.

CSS Styling

The CSS file (styles.css) styles the elements:

  • The #color-box is given a fixed width, height, and default background color.
  • The #change-color-btn is styled to look like a button with a hover effect.

JavaScript Logic

The JavaScript file (script.js) contains the logic for generating random colors and updating the box color. Here's how it works:

  1. Wait for the DOM to Load: The script waits for the DOM content to be fully loaded before executing any code. This ensures that all elements are available for manipulation.

    document.addEventListener("DOMContentLoaded", function () {
      // Code goes here
    });
  2. Select Elements: The script selects the color-box and change-color-btn elements using document.getElementById().

    const colorBox = document.getElementById("color-box");
    const changeColorBtn = document.getElementById("change-color-btn");
  3. Generate Random Color: The getRandomColor() function generates a random hexadecimal color code. It works by:

    • Creating a string of valid hexadecimal characters (0123456789ABCDEF).
    • Looping 6 times to pick random characters and appending them to a # symbol.
    function getRandomColor() {
      const letters = "0123456789ABCDEF";
      let color = "#";
      for (let i = 0; i < 6; i++) {
        color += letters[Math.floor(Math.random() * 16)];
      }
      return color;
    }
  4. Add Event Listener: An event listener is added to the button. When the button is clicked, the getRandomColor() function is called, and the background color of the color-box is updated.

    changeColorBtn.addEventListener("click", function () {
      colorBox.style.backgroundColor = getRandomColor();
    });

How to Run the Project

  1. Clone the repository or download the files.
  2. Open the index.html file in a web browser.
  3. Click the "Change Color" button to see the box change color.

Example

  • Initial State: The box has a default color (e.g., #3498db).
  • After Click: The box changes to a random color (e.g., #e74c3c, #2ecc71, #9b59b6).

Screenshot

Color Changer Screenshot

License

This project is open-source and available under the MIT License.


JavaScript Logic Explained

Key Steps

  1. DOMContentLoaded Event:

    • Ensures the JavaScript code runs only after the HTML is fully loaded.
    • Prevents errors caused by trying to manipulate elements that don't yet exist.
  2. Random Color Generation:

    • Hexadecimal colors are represented as a # followed by 6 characters (0–9, A–F).
    • The getRandomColor() function builds this string by randomly selecting characters.
  3. Event Handling:

    • The click event listener on the button triggers the color change.
    • The style.backgroundColor property updates the box's background color.

Why This Works

  • The Math.random() function generates a random number between 0 and 1.
  • Multiplying by 16 and using Math.floor() ensures the number is an integer between 0 and 15.
  • The letters string maps these numbers to valid hexadecimal characters.

Future Improvements

  • Add a feature to copy the generated color code to the clipboard.
  • Allow users to input a specific color manually.
  • Add animations for smoother color transitions.

About

JS-DOM-Challenge

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages