Using Jquery to create a fading effect when changing the color by checking a checkbox

I'm struggling with adding a fading effect to change the background color of the body element when a checkbox is checked. The color changes successfully, but I can't figure out how to make it fade smoothly.

Here is the HTML code:

<div class="container">
            <label class="switch" for="checkbox">
            <input type="checkbox" id="checkbox" />
              <div class="slider round"></div>
            </label>
</div>

And here is the JQuery script:

$(document).ready(function(){
      $('#checkbox[type=checkbox]').on('change', function(){
           if($(this).prop('checked')){
              $('body').css('background-color', 'red');
           }
           else {
               $('body').css('background-color', '#181818');
           }
    });
});

Answer №1

Here is a CSS snippet for creating a smooth half-second transition effect on the background color of the body element.

body {
    transition: background-color 0.5s;
}

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

Deactivating form elements using jQuery

I am attempting to utilize jQuery in order to disable a form element once a checkbox is clicked, but for some reason it's not working properly. Can someone help me identify the issue? $('name="globallyAddTime"').click(function() { if ($ ...

retrieving data from identical identifiers within a loop

Within my while loop, I am retrieving various dates for each event. <?php while( have_rows('_event_date_time_slots') ): the_row(); ?> <div> <h3 class="date-<?php echo $post->ID?>" name="tttdate<?php e ...

Is it possible to direct the user to a particular link upon swiping to unlock?

Hello everyone, I could use some assistance with this code. I am looking to redirect someone to a specific page when they swipe to unlock and automatically transfer them to another page when the swipe ends. Any help is greatly appreciated. Thank you! $ ...

Get alerts from Twitter pushed to my site

Is it possible to create a web application that utilizes JavaScript to receive notifications from Twitter? I want my website app to send me notifications whenever a person I follow posts a new article. I'm having difficulty with this task and would gr ...

How to style the first column of the first row in a table using CSS without affecting nested tables

Having some challenges with CSS selectors, I'm hoping to find some assistance here. Presented with HTML code that resembles the following: <table class=myTable> <tr> <td>this is the td of interest</td> </tr> ...

Managing Visual Studio Code Extension Intellisense: A Guide

I am looking to create an extension I recommend using "CompletionList" for users. Users can trigger completion by running "editor.action.triggerSuggest" The process of my extensions is as follows: Users input text If they press the "completion command," ...

Removing CSS from a button inside a jQuery Mobile popup: a step-by-step guide

Within a jQM dialog, I have a pair of buttons: <a href="#" data-role="button" data-inline="true">Yes</a><a href="#" data-role="button" data-inline="true" data-rel="back">No</a> These buttons are automatically styled by jQM, incorp ...

Is there a way to retrieve both the name of the players and the information within the players' profiles?

Just dipping my toes into the world of Javascript and jQuery while attempting to create an in-game scoreboard for Rocket League, I've hit a bit of a roadblock. Here's the console log output of data from the game: I'm particularly intereste ...

How can one achieve an explosion effect using jQuery?

Hey there! Here's a question to ponder... Let's assume we have a JavaScript variable set up like this: let myString = 'x,y,z'; let separated = myString.split(','); Now, imagine we have an HTML element like this: <ul id= ...

Clicking on an element deactivates its hover state

While experimenting on this JSFiddle, I noticed that after clicking one of the boxes, the hover state becomes inactive and doesn't show up again. These three boxes have their background set using css background-image. The click event is attached usin ...

The issue arises when a continuous use of angularjs directives linked with an external template fails to display correctly upon the addition of new

In the code snippet below, you'll find a fiddle that displays 3 columns of numbers that increase in value. These columns represent directives with different templates: one inline, one preloaded, and one from an external template. When you click on the ...

I encountered an error stating that ".then" is not defined during my attempt to save

Just starting out with node.js and I encountered an issue: TypeError: Cannot read property 'then' of undefined This is the code snippet causing the problem: router.post("/signup", (req, res) => { const userRegister = new UserRegister({ ...

Maintain consistent width of a page across different mobile devices

Currently, the content on my page does not have any viewport settings. It is simply using a 25% left and right margin for the entire content area. The issue arises when accessing the page from a mobile device, as the width adjusts to match the screen size ...

Guide to Increasing Table Index within Nested Loop in Vue.js 3

How can I increment table indices within a nested loop in Vue.js 3? I am currently working with two-level deep arrays using Vue.js, and I need an index that starts at 1 and increases for each item in the top-level array. The desired output is as follows: ...

What is the best way to transform this JSON data into an HTML table using JavaScript?

I am looking to dynamically create an HTML table based on changing json data. I want the table to maintain consistent alignment and width, similar to the example shown here. How can I convert the incoming json data into a well-structured HTML table? [ ...

Tips on preventing CSS issues when the background of the second line overlaps the text of the first line

I'm currently working on creating a website at I've noticed that the background of the second line's title element is overlapping the first line. Any suggestions on how to prevent this from happening? Thank you for your help! ...

Using Typescript to Toggle a Side Navigation (Sidenav)

Seeking assistance with implementing a toggle button for a side navbar in Angular and Typescript Nav <nav *ngIf="nav.visible" class="sidebar col-xs-12 col-sm-4 col-lg-3 col-xl-2 bg-faded sidebar-style-1 sidenav pt-3 mt-1 pr-0 pl-0"> <ul class="n ...

What is the best way to save pictures into an array using JavaScript?

I am seeking advice on whether it is feasible and the process to follow for storing an image in an array. As someone who is new to JavaScript, I would greatly appreciate a detailed explanation of how this can be achieved along with the syntax involved. My ...

In Bootstrap 5, clicking inside a dropdown should not cause it to open or close unexpectedly

Check out the code snippet below: <html> <head> <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7d1f1212090e090f1c0d3d48534e534e">[email protected]</a>/d ...

Creating equal spacing between divs and the edges of the container

I need to arrange a set of divs equidistant from each other. My goal is to have the maximum number of divs fit side by side with equal spacing between them and the container edge. While I almost achieved this with the code below (credit to another post), ...