`Change the background color of a webpage using JQuery when a button is clicked`

Excited to dive into jquery, I'm attempting to switch the page background colour with a button click.

Despite finding numerous similar questions, I need guidance on changing the background specifically. All help appreciated, thanks! - Freddie

HTML:

<!DOCTYPE HTML>
<html lang="en">

<head>
    <meta charset="utf-8>
    <link rel="stylesheet" type="text/css" href="style.css">
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script type="text/javascript" src="script.js"></script>
</head>

<body>
    <section class="text">
        CLICK THE BUTTON TO<br> CHANGE THE BACKGROUND!
    </section>

    <button id="btn">CLICK ME!</button>

</body>
</html>

CSS:

body {
    height: ;
    background: #D8D8D8;
}

.text {
    color: #686868; 
    font-family: arial;
    font-size: 2em;
    text-align: center;
    margin-top: 50px;
    margin-bottom: 20px;
}

#btn {
    margin-left: 550px;
    width: 200px;
    height: 100px;
    font-size: 2em;
    border-radius: 10px;
    background: #ffffff;
}

#btn:hover {
    background: #ff0000;
    color: #ffffff;
}

JS:

$(button).click(function() {
    $(this).css('background', '#ff0000');
)};

Answer №1

$( "#change_color_theme" ).on( "click", function() {
  $("body").css("background-color","#000000");
});

Click here for a demo.

Answer №2

Transforming the code:

$(button).click(function() {
    $(this).css('background', '#ff0000');
)};

To achieve this result:

$(document).ready(function(){
    $('#btn').click(function() {
        $('body').css('background', '#ff0000');
    )};
});

Answer №3

Consider modifying the

//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js
link to
http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js
when conducting local website testing.

If you include //, it will use the protocol that is being used to access the page (file:// for local testing).

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

I'm noticing that the top border for my span is smaller than the bottom one. What could

I am struggling to create an arrangement of three triangles in a line, with the first and third pointing up and the middle one pointing down. The upper triangle seems to be too small - any ideas on how to adjust this? Is there another way to achieve this ...

Using a contenteditable div with a set maximum character limit, powered by jQuery

Here is the challenge at hand - I am looking to restrict input to no more than N characters in a contenteditable div using either native JS or JQuery. For example, if a user inputs 10 symbols, they should not be able to input any more and can only clear ...

Require the utilization of both versions of jquery 1.7.2 and 1.8.2

I am facing a dilemma where I need to use two different versions of jquery on the same page. I require version 1.7.2 for a specific function that retrieves data from a database based on a selection change in PHP, and I also need version 1.8.2 for form vali ...

When using jQuery Ajax, the data being sent in the post request may include previous

Imagine sending the following JSON String in JsonInput First: { "JobNumber": ["208-01"], "Location": ["003118"], "HostName": "TestHOST", "WoNumber": "4268-6" } The Ajax response is succ ...

Adjust the starting color of a mat-input in Angular 9

I am encountering an issue with the styling of a standard matInput field within a mat-form-field, all contained within a [formGroup] div. The dark background of the parent element is causing visibility problems with the default dark text color of the input ...

What is the most efficient method for implementing uniform rounded corners in HTML/CSS across different web browsers?

Is there a simple way to achieve cross-browser compatibility without a lot of tedious work? I'm looking for a solution that works effortlessly on IE7+, FF2+, and Chrome, without resorting to using tables which may be outdated. Is there a middle ground ...

Creating a website with a responsive design that perfectly adapts to different screen sizes, starting with my 1920x1080

've been working on designing a website using Dreamweaver CS6, and during this process, I encountered some challenges. Initially, I set everything up based on hard pixel values for div and image sizes, but realized that this caused severe breaks in th ...

What is the best way to create a popup that appears next to a button and above another element?

pre-prompt: https://i.sstatic.net/0l32k.png post-prompt: https://i.sstatic.net/MzRUu.png I am completely clueless on how to achieve this, can anyone provide guidance? ...

Trouble with jQuery UI add/remove class duration feature

I have implemented Jquery UI with the addClass/removeClass function successfully. However, I am facing an issue where the class changes without the specified duration. Below is the relevant code snippet: <script src="https://ajax.googleapis.com/ajax/li ...

Email signature becomes distorted when replying to messages

I am encountering an issue with my email signature that is causing it to become distorted when replying in Outlook. The problem arises when I send an email with the signature from Outlook 2016 on Mac to Outlook 2007 on Windows – initially, everything lo ...

"Failure encountered while trying to fetch JSON with an AJAX request

I am facing an issue with an ajax request. When I make the request with the property dataType: 'json', I get a parsererror in response. My PHP function returns the data using json_encode(), can someone assist me? On the other hand, when I make th ...

Server failing to process Ajax POST request

I'm currently using ajax to send a post request to my C# code in the code behind of my webpage to save content from a tinyMCE editor to a file. The ajax function is triggered whenever the save button is clicked on the editor. Although I have successfu ...

I am looking for a setup where a checkbox triggers the opening of the next accordion

I am encountering a problem with the bootstrap accordion feature. I am looking to have the first accordion nested inside a checkbox, so that when the checkbox is clicked, the next accordion automatically opens. If the checkbox is not clicked, the next ac ...

Ensure that the background div extends all the way to the bottom of the page

Is there a clever CSS technique that allows a div to extend all the way to the bottom of the screen without displaying any visible content inside? Or am I better off using JavaScript for this purpose? Thank you, Richard ...

What is the process for creating a hover linear wipe transition using CSS/JS?

It's worth noting that I can't simply stack one image on top of the other because I'll be dealing with transparent images as well. preview of linear wipe ...

What is the best method for displaying a custom print and paginated preview complete with Page Boxes using a combination of Javascript and CSS

I am looking to display a scrollable print preview of HTML content on a single page. Please carefully review the following information and offer suggestions for a solution. When given dynamic raw data that can be extensive, is it possible to utilize cust ...

Using Material-UI with React to solve the issue of DatePicker being hidden under modal

The MUI Parent Component includes a Page, followed by a modal also from MUI. Inside the modal, I am rendering the DatePicker component. // @mui import { DatePicker } from '@mui/x-date-pickers'; This is how it appears on the screen. <Contr ...

What is the best way to navigate to a specific element using jQuery?

I am having an issue with scrolling to a specific div at the top of my page. Even though I have buttons for other sections, when I scroll to the bottom and click on the button, it does not take me to the top of the page as intended. You can view the CodeP ...

Utilizing jQuery to place an element beside another and maintain its position

I need ElementA to be positioned next to ElementB as shown in this example. The key difference is that I want ElementA to move along with ElementB if the latter is relocated. Is there a way to keep a specific element fixed to another one? Re-calculating ...

How can I set up the datepicker to only display dates that are exactly 5 days ahead of the start date and 14 days before the end date?

Is there a way to display the date in the jQuery UI datepicker (specifically in the #id_erinnerung_am field) only if the date falls within "startdate + 5 days" and "enddate - 14 days"? Any other dates should be disabled. $(function() { $( ".vDateField ...