The color of the background does not remain changed permanently

I'm having an issue where the background color changes when I click a button, but reverts back almost immediately to its original color. How can I ensure that the new color stays permanently?

Here is a simple JavaScript function you can use:

function setbackground(color)
{
  document.body.style.backgroundColor = color;
}

To implement this in your HTML code, add the following line:

<input id="bigbutton" type="submit" value=""
  style="background: url(moods/rocking.png) no-repeat center" 
  onclick="setbackground('#B84DB8')"/>

Answer №1

Modifying the DOM will alter the current page's appearance.

When you click a submit button, it triggers the form submission process.

Upon submitting a form, a new page is fetched from the server, potentially identical to the previous one but without retaining any local changes made to the DOM.

Essentially, you're updating the current page only to discard those changes immediately and load a fresh version.


To achieve your desired outcome, consider the following options:

  • Prevent form submission using JavaScript (commonly done by returning false in an onclick attribute, although addEventListener is now preferred over inline attributes). Alternatively, question the use of a submit button if unconditional prevention is necessary, favoring a regular button instead (yet maintaining progressive enhancement).
  • Utilize the name/value of the submit button to prompt the server to generate a specific HTML document (e.g., with custom colors) rather than relying on JavaScript for this task.
  • Save color change data locally (e.g., in a cookie or localStorage) so that JavaScript can retrieve it upon each page load and reapply the changes as needed.

In response to a comment:

Yes, I must submit a form and then adjust the background color afterward.

If that is the case, the first option may not suffice unless supplemented with Ajax functionality.

Opting for server-side modifications to implement the color change would likely be the least disruptive solution.

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

Creating a split background with dual hues in a VUE project

I'm new to learning VUE and I'm trying to create a page where two different colors connect on the left and right with the same height. However, I'm running into issues where the colors don't align unless I add content to the right side. ...

What is the reason that utilizing $("input").first() does not function properly on Google Forms?

While practicing with Selenium, I encountered an issue while using this Google form. Specifically, I found that I couldn't select the first input field by using $("input").first() To confirm this issue, I turned to the web browser (Firefox) console t ...

Is it possible to utilize LESS to dynamically read the width?

My LESS code looks like this: &.settings-prompt .panel { margin-left: -190px; width: 380px; height: 200px; } &.create-playlist-prompt .panel { margin-left: -165px; width: 330px; height: 180px; } I want the .panel to have ...

Having difficulty executing the npm test command for my Angular 2 application

I am currently working on an Angular 2 application, and I am fairly new to it. I have set it up with Cordova app and run it through npm. The app starts without any errors and runs smoothly. However, when I try to run the tests using node (i.e., npm test), ...

Tips for obtaining the retrieved URL from an ajax call

How can I extract only the returned URL from an ajax request? I have tried implementing it like this: $.ajax({ type: "GET", dataType : "jsonp", async: false, url: $('#F ...

What should I do when using _.extend() in express - override or add in fields?

When an object is extended by another object with values set for some of the extended fields, will it be rewritten or will the new values be added? For example: const PATCH_REQUEST_SCHEMA = { 'type': 'object', 'title' ...

Query parameter is not defined

Can anyone assist me with extracting the ean from the following URL: This NodeJS server processes the request in the following manner: const http = require('http') const port = 3000 const requestHandler = async (request, response) => { ...

Issue with invoking controller action in MVC4 via AJAX

Below is the method in my controller: public JsonResult GetRights(string ROLE_ID) { var result = db.APP_RIGHTS_TO_ROLES.Where(r => r.FK_ROLE_ID.ToString() == ROLE_ID).Select(r => r.APP_RIGHTS).ToList(); return Json(re ...

Is there additional cushioning beneath an image within a div element?

It appears that my div containing an image has extra padding at the bottom for some unknown reason. You can view the JSFiddle here: http://jsfiddle.net/KSs7V/ Any suggestions on how this padding might have been added and how to correct it? CSS: .artist ...

Are all elements still displaying the menuBar style? Using the PHP include function, <?php include... ?>

Here is the Menu Bar code I am using: <!DOCTYPE html> <html> <link href = menuBarStyle.css rel = "stylesheet"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <div class = "menu"> ...

How can I connect a database to an HTML website on a local server (XAMPP)?

For my university project, I've developed a website that now requires the addition of a database. I'm looking to figure out how to integrate a database using XAMPP for the website. ...

Guide to automatically blur the input field and toggle it upon clicking the checkbox using Angular

<input (click)="check()" class="checkbox" type="checkbox"> <input [(ngModel)]="second_month" value="2019-09" type="month" [class.blurred]="isBlurred">> I need the second input(type=month) field to be initially blurred, and only unblur ...

Issue with Chrome when using angled CSS gradient backgrounds

I'm encountering a problem with a CSS gradient background on a div that is set to 100% width and height. This issue only seems to be happening in Chrome. I have some content placed over the div, and when I hover over links within that div, the gradien ...

Utilizing ES6 Map Reduce to flatten an array with mapping and padding, sourced from the Redux state

Currently, I am developing a React/Redux application using ES6 and looking for an efficient method to transform this dataset: [ {total: 50, label: "C1"}, {total: 120, label: "C2"}, {total: 220, label: "C4"} ] Into something similar to the structu ...

Design a clear button for MUI autocomplete feature

I'm currently working on a project where I need to implement a button that clears the MUI autocomplete value and removes the data from the UI. Although I've managed to delete the data from the UI with the code provided, the onChange value remain ...

Exporting MySQL data to MS Excel is functioning smoothly on the local environment, however, it is encountering difficulties when

<title>Orders Export</title> <body> <?php header('Content-Type: application/xls'); header('Content-Disposition: attachment; filename=download.xls'); $con = mysqli_connect('localhost','suresafe ...

Best practice for including the language tag in Next.js Head tag

I'm struggling to find a clear example of how to use the language tag with Next Head. Here are two examples I have come across. Can you help me determine which one is correct, or if neither are appropriate? Just a reminder: it doesn't need to be ...

Switch out a new js file every time the page is reloaded

I have a JS file with various objects (var), and I'm looking to dynamically load a different object each time the page is loaded. For instance, upon page load, a new object 'temp' should be created with data from one of the objects in the JS ...

The JSON response from Rails containing multiple lines is not being parsed accurately

I am currently working on a Rails application with a json response using show.js.erb. { "opening": "<%= @frame.opening %>", "closing": "<%= @frame.closing %>"} An issue I encountered is that when @frame.opening contains multiple lines, jQuer ...

Unexpected Display Issue with Angular Select Option

My goal is to implement the Select-Option Input as depicted in the first image link here: https://i.sstatic.net/SVT8R.png However, my current output looks different and I suspect that there may be a conflict between two CSS libraries. It's worth noti ...