JQUERY Tutorial: Building a Color Picker Project 🎨💻

beginner
13 min

JQUERY Tutorial: Building a Color Picker Project 🎨💻

Welcome to our comprehensive guide on jQuery Color Picker Project! This tutorial is designed for both beginners and intermediates, so let's dive right in.

What is jQuery? 💡

Before we start, let's understand what jQuery is. It's a JavaScript library that simplifies HTML document traversing, event handling, and animation. In this tutorial, we'll learn how to build a color picker using jQuery, making your websites more interactive and engaging.

Getting Started 📝

First, let's include jQuery in our HTML file:

html
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

Now, let's create our color picker HTML structure:

html
<div id="colorPicker"> <div class="colorBox"></div> <div class="colorValues"> <span id="hexCode"></span> <span id="rgbCode"></span> </div> </div>

Understanding the HTML Structure 💡

  • #colorPicker is our container holding the color box and color values.
  • .colorBox is the actual color box.
  • .colorValues is the container for the hexadecimal (#hexCode) and RGB (#rgbCode) color codes.

Creating the Color Picker Function ✅

Now, let's write the JavaScript function to change the color box and display the corresponding hexadecimal and RGB values:

javascript
// Color Picker Function function setColor(color) { // Set color box background $('#colorPicker .colorBox').css('background-color', color); // Set hexadecimal code $('#hexCode').text(color.toString(16).toUpperCase()); // Set RGB code const rgb = colorToRgb(color); $('#rgbCode').text(`RGB(${rgb.r}, ${rgb.g}, ${rgb.b})`); } // Convert color to RGB function function colorToRgb(color) { // Convert hexadecimal color to RGB // Reference: https://stackoverflow.com/a/13707932 const r = parseInt(color.toString(16).substr(0, 2), 16); const g = parseInt(color.toString(16).substr(2, 2), 16); const b = parseInt(color.toString(16).substr(4, 2), 16); return { r, g, b }; }

In the code above, we've created two functions:

  1. setColor: This function changes the color box background and updates the hexadecimal and RGB color codes.
  2. colorToRgb: This function converts the hexadecimal color to RGB values.

Interacting with the Color Box 💡

Now, let's make our color box clickable so that we can change colors:

javascript
// Initialize color picker let currentColor = "#000000"; // Black by default setColor(currentColor); $('#colorPicker .colorBox').click(function () { // Change color and update color picker currentColor = randomColor(); setColor(currentColor); }); // Generate random color function function randomColor() { return '#' + Math.floor(Math.random()*16777215).toString(16); }

In the code above, we've:

  1. Initialized the color picker with a default black color.
  2. Set the click event on the color box to generate a random color and update the color picker.
  3. Created a randomColor function to generate a random hexadecimal color.

Wrapping Up 📝

Congratulations! You've built a simple color picker using jQuery. This project showcases how to create interactive elements, handle events, and manipulate the DOM in jQuery.

Quick Quiz
Question 1 of 1

What is the JavaScript library we are using in this tutorial?

Now, try to modify the color picker to accept user input and create a more customizable color picker. Happy coding! 🚀✨