Delving into the World of CSS

I have been working on changing the background image using this JavaScript code, but I am not sure what to reference in the CSS file. Despite reading through everything, my screen still remains blank.

$(function() {
var body = $('body');
var backgrounds = new Array(
'url(images/bg.jpeg)',
'url(images/bg2.jpeg)',
'url(images/bg3.jpeg)'
);
var current = 0;

function nextBackground() {
body.css(
'background',
backgrounds[current = ++current % backgrounds.length] 
);

setTimeout(nextBackground, 10000);
}
setTimeout(nextBackground, 10000);
body.css('background', backgrounds[0]);
});

In addition, I have included jQuery by linking to the Google CDN.

Thank you so much for your assistance.

T

Answer №1

There may not be an official answer for this question, but I will provide a response based on the comments.

The issue stemmed from the use of ‘curly quotes’ instead of 'straight quotes.'

Answer №2

The provided code contains errors such as incorrect quote marks for strings and a missing comma after the second background value.

$(function() {
  var body = $('body');
  var backgrounds = new Array(
    'url(images/bg.jpeg)',
    'url(images/bg2.jpeg)',
    'url(images/bg3.jpeg)'
  );
  var current = 0;

  function nextBackground() {
    body.css(
      'background',
      backgrounds[current = ++current % backgrounds.length]
    );

    setTimeout(nextBackground, 10000);
  }
  setTimeout(nextBackground, 10000);
  body.css('background', backgrounds[0]);
});

You can view a demo showcasing the corrected version with colors instead of background images: https://jsfiddle.net/kb58tvo9/1/

A cleaner version of the code can be found here: https://jsfiddle.net/kb58tvo9/2/

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

Obtain JSON information and integrate it into an HTML document with the help of

I am currently working on a PHP/JSON file named users-json.php. <?php include_once('../functions.php'); if (!empty($_GET['id'])) { $GetID = $_GET['id']; $query = "SELECT Username, Firstname WHERE UserID = :ID"; $stmt = $d ...

Is there a way to change the background color of product blocks on Shopify in a dynamic

Is there a way to customize the background colors of product blocks on Shopify by modifying the shop code? Take a look at this example: Mockup showing alternating block backgrounds Currently, all product blocks in the grid have a black background with wh ...

The function jQuery(" ").datepicker({ }) does not function correctly in Internet Explorer versions 7 and 8

Having Trouble with Datepicker jQuery(function() { jQuery("#fromDatepicker").datepicker({ changeMonth : true, changeYear : true, dateFormat: 'mm/dd/yy' }); }); jQuery(function() { ...

On an iPad, the clickable area outside a div element is not functioning properly

I'm attempting to close a div block on iPad when users click outside the div, but it doesn't seem to be working as expected. Could someone please point out what I might be doing incorrectly? if (isIOSDevice()) // using a custom function ...

The webpage automatically reloads when triggering a jQuery ajax request to an ActionResult

When I try to add a comment on my page, it refreshes the entire page. What am I missing? I want the comments on the Details page to update dynamically without having to refresh the whole page. I am attempting to achieve something similar to how comments ...

Leveraging the outcome of a for loop in order to set a condition within an else if statement

How can I condition my third else if statement based on the result of a for loop? //If player clicks centre on first move go in corner square if (current[4] === playerToken && this.state.stepNumber === 1) { let move = c ...

When PHP echo of json_encode triggers an error, AJAX status 200 will be raised

Despite everything running smoothly in the program below, an AJAX error is triggered: javascript: var data = { email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="026f6742656f636b6e2c616d6f">[email protect ...

AngularJS ng-include nested form configuration

My application utilizes nested ng-includes. The outer include is a login screen for one application while the inner ng-include includes two templates. The login screen is designed in two steps where the email is checked first and then the password entered. ...

Sinon's fakeTimers failing to trigger

I'm encountering an issue with sinon's fakeTimers while working in a setup that includes Marionette.js, underscore, and chai test runner. Strangely, when I place a breakpoint in Chrome and step through the code, my timer functions as expected. Ho ...

Vuetify: how to disable the color transition for v-icon

My menu includes both icon and text items, with hover color styled using the following CSS: .v-list-item:hover { background: #0091DA; } .v-list-item:hover .v-list-item__title, .v-list-item:hover .v-icon { color: white; } The problem is that the ...

Troubleshooting Event Tracking Problems with Brave Browser on PostHog

After successfully implementing Posthog with React and testing it on Chrome and Firefox, we encountered issues when trying to test it on Brave/Microsoft Edge Browsers. It appears that the default ad blocker feature in these browsers is causing the problem. ...

Is it possible to rearrange the node_modules directory?

Within the node_modules directory, there exists a large and extensive collection of modules. These modules are often duplicated in various sub-folders throughout the directory, with some containing identical versions while others differ by minor versions. ...

Revamping status and backend systems

Seeking advice on the most effective method to execute a HTTP PUT request within my react application. A Post component is responsible for fetching data from https://jsonplaceholder.typicode.com/posts/1 and displaying it. There's another component na ...

Simulate a failed axios get request resulting in an undefined response

I'm having an issue with my Jest test mock for an axios get request, as it's returning undefined as the response. I'm not sure where I might be going wrong? Here is the component with the axios call: import {AgGridReact} from "ag-grid- ...

Arrange the table by column following a service request

Is there a method to organize the table below once it has been loaded? I am utilizing Google API to retrieve distance matrix data, but I want the table to be sorted by distance. However, this sorting should take place after the Google service call is comp ...

Tips on updating arrow button icon when clicked using jquery

I am currently working on a project where I have a button icon that I want to change upon clicking it. I am using the following jQuery code: <script> $('div[id^="module-tab-"]').click(function(){ $(this).next('.hi').sl ...

"Data is not defined" error message is triggered when using jQuery DataTable Row Details

While utilizing jQuery Data Tables to construct a datatable with row details, I encountered an error in jquerydatatables.js: data is undefined The JavaScript code being used is: $(document).ready(function() { var dt = $('#tbl_cheque_history' ...

Create a servlet that generates a PDF file and displays it in a fresh browser tab. However, the PDF document will be blank

I have a servlet that currently provides a PDF byte array: byte[] pdf = myService.getPdf(myArgs); response.setContentType("application/pdf"); response.setHeader("Expires", "0"); response.setHeader("Cache-Control", "must-revalidate, postcheck=0, pre-check= ...

Navigating through different pages and updating the URL using AJAX requests

Greetings! I am currently in the process of learning ajax/jquery, so please forgive any potential rookie mistakes. As I scoured the internet for a solution to my current dilemma, I stumbled upon bits and pieces of information here and there, but I am stru ...

Introduction to React with Typescript: Greeting the World

I am attempting to create a Hello World React application with Typescript. Below is the code I have written. Method 1 works without any issues, but method 2 throws an error. Method 1 - TypeScriptComponent.tsx import React from 'react' import Re ...