Cascading style sheet

JavaScript Change Background Color on Click

In this tutorial, we are going to change the background color of a webpage to the one supplied by the user in the input field.

Let’s get Started!

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Change JavaScript Background Color on Click</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-0evHe/X+R7YkIZDRvuzKMRqM+OrBnVFBL6DOitfPri4tjfHxaWutUpFmBp4vmVor" crossorigin="anonymous">
    <style>
    .colorContainer {
      display:flex;
      flex-direction: row;
      justify-content: center;
      align-items: center;
    }
    </style>
  </head>
  <body>
    <div class="container d-flex justify-content-center colorContainer">
    <form>
    <input id="colorName" type="text" class="form-control-lg" placeholder="Enter color name or code" />
    <button type="button" id="btn" class="btn btn-primary btn-lg" >

Result:

See also

CSS Dropdown Menu

Share
Published by
codeflare

Recent Posts

Common React Errors

React makes building user interfaces easier, but certain errors appear repeatedly, especially when working with…

13 hours ago

C Programming Cheat Sheet

The C programming language is a foundational, general-purpose computer programming language created by Dennis Ritchie at Bell Labs in 1972. Originally developed to…

20 hours ago

Homebrew Tutorial

What Is Homebrew? Homebrew is a package manager for macOS and Linux that makes it easy to…

5 days ago

10 Ways to Loop Through a JavaScript Array

JavaScript gives you several ways to iterate over an array. Some are better for simple…

6 days ago

Build a Node JS Web Server

A web server is the software responsible for receiving requests from clients, processing those requests,…

1 week ago

Shell Prompt Tutorial

A shell prompt is the text displayed by a command-line shell to show that it is ready…

2 weeks ago