What could be causing the background color change to fail in this JavaScript code?

I'm trying to enable the background color change feature upon clicking a button based on the selection made from a dropdown. Below are the HTML and JavaScript snippets for reference.

<!DOCTYPE html>
<html>
    <head>
        <title>Color Change Demo</title>
        <script>
            function changeBackground(){
                var selectedColor = document.getElementById("selColor");
                var colorValue = selectedColor.value;
                document.body.style.backgroundColor = colorValue;
            }
        </script>
    </head>
    <body>
        <form action="">
            <fieldset>
                <select id="selColor">
                    <option value="FFFFFF">White</option>
                    <option value="FF0000">Red</option>
                    <option value="FFCCFF">Orange</option>
                    <option value="FFFF00">Yellow</option>
                    <option value="00FF00">Green</option>
                    <option value="0000FF">Blue</option>
                    <option value="663366">Indigo</option>
                    <option value="FF00FF">Violet</option>
                </select>

                <input type="button" value="Change Color" onClick="changeBackground()"/>
            </fieldset>
        </form>
    </body>
</html>

Despite my efforts, I can't seem to get it to work properly. Any assistance would be greatly appreciated.

Thank you

Answer №1

If you're using hex to represent RGB in CSS colors, make sure to include a "#" at the beginning.

Answer №2

There is a slight error in the code where the '#' symbol is missing. Here is the corrected version:

document.body.style.backgroundColor = "#"+color;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Discover the flawless way to transmit client geolocation to a server. Encounter an intriguing hurdle: Unable to access undefined properties (specifically 'loc') while reading

I encountered an error that says "TypeError: Cannot read properties of undefined (reading 'loc')". This error was triggered when I attempted to pass the user's location to a server through a POST request. In my HTML file, I have included th ...

Utilizing Lodash's uniqWith function, it allows us to specify how we want to

Currently, I am utilizing the lodash uniqWith method to eliminate duplicate items with the same id from my array. However, the lodash method retains the first duplicated item, whereas I actually want to keep the last duplicated item. Is there a way to ac ...

Steps to incorporate fancybox in a PHP MySQL gallery: Once the gallery is established, ensure that the path points to

After creating a gallery using PHP and a MySQL database, I encountered an issue when attempting to integrate fancybox. The gallery successfully fetches images from the upload folder, but fancybox does not use the default fancybox class. Below is a snippet ...

How to Develop a WebSocket Client for Mocha Testing in a Sails.js Application?

I've been attempting to utilize the socket.io-client within the Mocha tests of my Sails JS application for making calls like the one shown below. However, the .get/.post methods are not being invoked and causing the test case to time out. var io = re ...

Use PHP to process a form submission and use AJAX to send a response back to the user

Currently in the process of developing a website and I am seeking guidance on how to utilize ajax to submit form data instead of relying on the html action attribute. A PHP script has been written that may produce different outcomes, and I want ajax to not ...

Exploring the world of Django, JavaScript, and intricate form designs

I am currently developing an application in django that allows users to create and modify flowcharts for process/procedure control. The app consists of three main models: Procedure, Step, and Transition. All the model relationships have been established a ...

Connect my asp.net grid view to JavaScript using AJAX

I am seeking guidance on how to bind my gridview from JavaScript in an ASP.NET web forms application. My objective is to click a linkbutton within my gridview and have it trigger a modalpopup that displays another gridview. Here are snippets of my code s ...

JavaScript code for extracting the value of a specific table cell from the provided screenshot

Looking at the image below, as someone new to JavaScript development, I have two questions. First, how can I directly retrieve the value of the second td from $('#cart-subtotal-order.total.subtotal td') in JavaScript code? Secondly, I need to kno ...

Making a tooltip in Angular programmatically

When hovering over an SVG element, I want to display a tooltip. I prefer the tooltip to be an Angular component for streamlined UI development. The challenge arises when the SVG element is dynamically generated, making it difficult to create a template r ...

Searching for a pattern and replacing it with a specific value using JavaScript

I need to find all occurrences of an unknown string within a larger string that is enclosed in brackets. For example, the string may look like: '[bla] asf bla qwr bla' where bla is the unknown string I need to locate. Is it possible to achieve th ...

Exploring ReactJS Design Patterns: Comparing Class Components to Custom Components

As I delve into best practices and design patterns using React, I find myself pondering the choice between two similar solutions: The first solution involves a class that does not extend a component. Its constructor returns an element set based on an obje ...

Tips for creating a deepCss selector for an input Textbox in Protractor

When I attempt to use sendKeys in an input textbox with Protractor, the element is located within a shadow-root and there are three separate input textboxes. ...

The onclick event seems to be malfunctioning on the website

My goal is to implement a modal box that appears when a user clicks a button and closes when the user interacts with a close button within the modal box. I have created two functions for this purpose: check() : This function changes the CSS of an element ...

Using setInterval() does not automatically fetch JSON data at a specified interval for me

Attempting to constantly update the latitude and longitude coordinates from a dynamic API has proven to be a challenge. Despite utilizing jQuery with a set interval of 1000 milliseconds, the data retrieval from the API does not occur as expected. While i ...

Perform an action when a button is clicked in a VueJs single-page application

How can I trigger the refreshMailList function when clicking on the mail-list component? This is my Vue instance with a custom mail-list component: Vue.component('mail-list', { props: ['inboxmail'], template: ` <div> ...

Converting HTML to JSON using jQuery

Currently, I am utilizing the jQuery template plugin to dynamically create HTML based on JSON data that user can then modify (and even change). My goal is to find a way to convert this HTML back into JSON format so that it can be saved back to my server. ...

Utilizing VueJS to Establish a Binding Relationship with Props

One of my Vue components is named Avatar.vue, and it is used to create an avatar image based on user-defined props. The parameter imgType determines whether the image should have rounded corners or not. Here is the code: <template> <div> & ...

Trouble obtaining AJAX result using onClick event

As a newbie to AJAX, I am still trying to grasp the concept. My task involves using an AJAX call to extract specified information from an XML file. Even after carefully parsing all tag elements into my JavaScript code, I encounter a problem when attempting ...

Can you explain the process for moving a div to align with another div?

Having just started with HTML and jQuery, I am looking to create three draggable divs stacked one below the other - div1, div2, and so on. I want to drag div1 into the position of div3, changing the order to div3, div1, div2. How can this be achieved? Ple ...

AngularJS Banner: Displaying Current Calendar Week and Increasing by 10 Days

I'm brand new to Angular and currently encountering some issues. Here's what I'm trying to create: I need to display the current Date: yyyy-MM-ss (Functional) I want to show the current Calendar Week: yyyy-Www (Not Working) When a butto ...