Switch up the Background using .css and jCarousel

I am attempting to create a dynamic gallery using jCarousel and Ajax. The thumbnails are being loaded from a '.txt' file. I am trying to achieve the following effect: When a thumbnail is clicked, the background of the body changes. This action should apply to all thumbnails, with each one changing the background to a different image. However, when I attempt to add dynamic URLs for different files, it doesn't work. If I manually change the URL to the correct path, it works fine. It seems that all thumbnails end up loading the same image from the specified path.

Below is my code:

  function mycarousel_getItemHTML(url)
{
    return '<img src="' + url + '" width="200" height="75" alt="" border="0" class="newbg" />';
};


jQuery(document).ready(function() {
    jQuery('#mycarousel').jcarousel({
        scroll: 2,
        itemLoadCallback: mycarousel_itemLoadCallback
    });

    $('.newbg').live("click", function(){
            $('body').css('background-image', 'url(' + url + ')');
    });
});

Does anyone have any ideas on how to modify this code to achieve the desired effects?

Answer №1

Here is the code I added:

$('.newbg').on("click", function(){
    var url = $(this).attr("src").replace("_t", "");
    var img = new Image();
    img.onload = function() {
    };
       img.src = url;                         
      $('body').css('background-image', 'url(' + url + ')');
    });

It functions correctly, thank you.

This should resolve the question at hand.

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 column in CSS that maximizes width without utilizing 100% or causing text wrap

Trying to keep a three-column div from spilling over the page can be tricky. The first two columns must not exceed 200px, while the third column should be at least 280px wide. The issue arises when there is a table in the third column with long sentences i ...

Numerous CSS/JS Dropdown Menus

I am seeking the ability to implement a dropdown through CSS in various locations within my HTML prototype. This implies that I require the capability to position multiple dropdowns anywhere on the page. Currently, I utilize this method to generate a sing ...

Leveraging AJAX to assist in PHP for data parsing

I am currently exploring the world of AJAX and grappling with a unique situation. I am in the process of developing an HTML app that will be integrated into a mobile application using PhoneGap. The main objective of my project is for the HTML page to con ...

What is the optimal method for saving and organizing data in SQL?

I currently have a MySQL table containing data that is displayed in an HTML table. Using JavaScript and drag & drop functionality, I am able to locally sort this table. My question is, what is the most effective method for saving these sorting changes? W ...

Bootstrap requires Visual Studio Code live-server plugin to function properly

I have been utilizing the Visual Studio Code IDE for my coding projects, along with a helpful plugin called "live server" that allows for quick checks of code changes. Everything was running smoothly with Bootstrap until I encountered an issue. When I att ...

Activate animation when a user clicks on a link

Hey there, I'm a bit unsure about how to word this and I'm still getting the hang of StackExchange so I hope I've posted in the correct place. Currently, I am developing a mobile HTML5 app that utilizes Phonegap/Cordova (as well as Bootstra ...

Rotating a CSS div causes the page to scroll horizontally

Currently, I am working on creating a unique design for the footer of my website. However, when implementing it, I encountered an issue that is causing a horizontal scroll (red background added for clarity). https://i.sstatic.net/K4vnV.jpg To address thi ...

mobile screens causing bootstrap columns to overlap

The code I am working with is shown below: <div class="container"> <div class="col-md-3 calendar"> </div> <div class="col-md-9"> <button></button> (more buttons...) </div> < ...

Tips for fixing the issue of disappearing top margins in elements while using CSS3 Pie in Internet Explorer 7

Hey there! I've been experimenting with a lot of CSS3 effects, but I'm running into issues trying to get the same effects to work on IE 7 and 8. I've been attempting to use CSS3 Pie to help with this. While CSS3 Pie has worked well for som ...

The impact is felt across every button in CSS

Why is this happening? When I hover over a button, all buttons apply the effect, which should not be the case. I only want the effect to be applied to the button that I am hovering over. .buttons{ font-family: arial; text-decoration: none; padding ...

Token Fields malfunctioning

I attempted to use a sample from bootstrap, but I am not having any luck with it. Is there anyone who can assist me in resolving this issue and understanding it better? Code Snippet: <input type="text" class="form-control" id="tokenfield" value="red, ...

Creating form inputs dynamically in HTML and then sending them to a PHP script programmatically

On my webpage, users can click a button to add new form inputs as needed. However, I'm running into an issue trying to access these new inputs in the PHP file where I submit the data. Here is the code snippet for the button within the form on the pag ...

Display an HTML image within a span tag and ensure it is aligned to the bottom

I am facing a layout issue with 2 images that are placed side by side within their tag . The sizes of the images are different, but they are both anchored at the top of their parent element. <span style="position: relative; left: 0; top: 0; vertical- ...

Joomla CSS styling malfunctioning

I've been exploring the creation of an in line menu using Joomla 1.6 and CSS. The issue arises when Joomla removes <span class="dmenu"> from the document before saving, despite having all cleanup options turned off. This led me to try a couple ...

A full-width div containing a child span is unable to be aligned to the right

After reviewing this fiddle, I'm struggling to get my menu aligned correctly at the right edge of the overall "card". Despite trying various methods like margins, right: 0, and float, nothing seems to work as expected. In some cases, I even end up los ...

How can an Embedded React + JSS component safeguard generic elements such as <button> and <p> from being affected by the page's style?

I am facing a challenge with a React component that is being embedded in various webpages, either through an extension or as a third-party tool. Most of the styling for this component is done using JSS, ensuring unique class names that cannot be overridde ...

Incorporating Masonry-inspired images into a website's design section

I have been working on incorporating a masonry layout of images into my website, but I am facing alignment issues. The images are simply stacking on top of each other instead of creating the desired masonry effect. Are there any tips on how to create a mas ...

React Native ellipsizeMode - Using two Text elements side by side

I am trying to achieve a layout similar to the following scenarios: Case 1: If the text is longer than the screen width +--------------------------------------+ | very long teeeeeeeeeeeeeeeee.. (1234)| +--------------------------------------+ Case 2: If ...

When using the post method in jQuery, it may send data that is unspecified in the request body

Greetings to everyone and I hope you have a fantastic start to the new year. I've been attempting to operate an API on localhost that will store input data in MongoDB. The issue arises when attempting to send data using the post method. The request ...

"Failure to Update: Ajax Auto-Refresh Table Failing to Refresh

I'm facing a tricky issue with my code that I just can't seem to resolve. Currently, I am using Ajax and setTimeout to retrieve data. The problem arises when the data doesn't refresh automatically (if I manually navigate to the page getdata. ...