JavaScript color viewer

JavaScript color viewer

This article example shares the specific code of JavaScript to implement the color viewer for your reference. The specific content is as follows

Achieve results

  • The box is initially white
  • Enter the color code in the input box, click to view the color, and the corresponding color will appear above
  • Click Restore to restore to the initial white color and clear the content of the input box.

Implementation Code

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <title>Color Viewer</title>
        <style>
            #color {
                width: 150px;
                height: 150px;
                background-color: #fff;
                border: 1px solid #000;
            }
        </style>
    </head>
    <body>
        <div id="color"></div>
        <input id="inp" type="text" placeholder="Please enter the color code..." />
        <button id="trans">View Color</button>
        <button id="rst">Restore</button>
    </body>
    <script>
        let trans = document.getElementById('trans');
        let color = document.getElementById('color');
        let inp = document.getElementById('inp');
        let rst = document.getElementById('rst');
        trans.addEventListener('click', () => {
            color.style.backgroundColor = inp.value;
        });
        rst.addEventListener('click', () => {
            color.style.backgroundColor = '#fff';
            inp.value = '';
        });
    </script>
</html>

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • JS implements a simple object-oriented color picker example
  • Simple radio background color selector code implemented by js
  • How to create a color picker based on 3 primary colors using javascript
  • js implements a color selector instance that can get different color values
  • JS code to dynamically change the background color of the web page when the mouse is selected
  • JS small function (button selection color) simple example
  • JavaScript gets the system's built-in color picker function (picture)
  • 5 Javascript Color Pickers
  • js color picker (compatible with firefox)
  • js color picker code [firefox not supported]

<<:  Detailed installation and use of SSH in Ubuntu environment

>>:  Summary of common functions of PostgreSQL regular expressions

Recommend

Detailed explanation of CSS margin overlap and solution exploration

I recently reviewed some CSS-related knowledge po...

Disable IE Image Toolbar

I just tried it on IE6, and it does show the toolb...

Basic operations of mysql learning notes table

Create Table create table table name create table...

Tomcat source code analysis of Web requests and processing

Table of contents Preface 1. EndPoint 2. Connecti...

Vue implements DingTalk's attendance calendar

This article shares the specific code of Vue to i...

Div adaptive height automatically fills the remaining height

Scenario 1: Html: <div class="outer"...

Learn how to use the supervisor watchdog in 3 minutes

Software and hardware environment centos7.6.1810 ...

DIV background semi-transparent text non-translucent style

DIV background is semi-transparent, but the words ...

Tutorial on installing MySQL 5.6 using RPM in CentOS

All previous projects were deployed in the Window...

Application of anchor points in HTML

Set Anchor Point <a name="top"><...

CSS multi-column layout solution

1. Fixed width + adaptive Expected effect: fixed ...

Example verification MySQL | update field with the same value will record binlog

1. Introduction A few days ago, a development col...

Vue two fields joint verification to achieve the password modification function

Table of contents 1. Introduction 2. Solution Imp...

js implements clock component based on canvas

Canvas has always been an indispensable tag eleme...