Before I make a selection, there is only one image displayed. But as soon as I choose that image, it transforms into a

When I select a radio button, should it replace the image with another one?

function getAirlinesById(idAirlines) {
            $.ajax({
                dataType: 'json',
                type: "POST",
                url: '{{route('get-baggage-airlines')}}',
                data:"airlines_id="+idAirlines,
                success: function(response){
                    console.log(response);
                    $("#baggageAirline").empty();
                    for(let i=0;i<response.length;i++){
                        let category = response[i].baggage.category;
                        switch(category) {
                            case 1:
                            category = "KG";
                            break;
                            case 2:
                            category = "PCS";
                            break;
                        };

                        $("#baggageAirline").append('<div class="col-3 list-baggage"><input type="radio" class="input-radio-costum" required onclick="getPrice('+response[i].price_in_SGD+')" onchange="changeBackground('+response[i].baggage.config_baggage_id+')" name="baggage" value="'+response[i].baggage.max_range+' '+category+'"><img src="{{asset("assets/image/baggage.png")}}" class="image-default" id="image-selected'+response[i].baggage.config_baggage_id+'"><p class="baggage-amount">'+response[i].baggage.max_range+' '+category+'</p><p class="text-center tcc">'+response[i].currency_symbol+' '+response[i].price_in_SGD+'</p></input></div>');

                    }
                },
                error: function(xhr, status, error) {
                    alert('ERR_CONNECTION_TIMED_OUT');
                    location.reload();
                }
            });
        }

I have attempted to implement this feature, but the selected image remains unchanged and all options are selected. Only one should be chosen as it uses a radio button.

function changeBackground(idBaggage){
        $('#image-selected'+idBaggage).css({
            'background-image' : "url('assets/image/baggageblack.png')",
            'margin-top' : '-30px',
        });
    }

Answer №1

To change the background image, you should create two separate classes

.default-background {
  background-image: url('/image-path');
}
.change-background {
  background-image: url('/new-image-path') !important;
}

Initially apply only the .default-background class to the element. When you trigger an onclick event to select a new image, then add the .change-background class which will override the default background image.

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

What is the best way to handle IE7 inaccurately calculating the width of floated elements?

Many times in my coding projects, I incorporate floated elements. Everything usually goes smoothly until Internet Explorer 7 decides to throw a wrench in the works. For instance, take a look at this code: HTML <div id="container"> <div id="e ...

Which comment widget/platform does 9GAG utilize?

I am in the process of developing a new website and I am looking for a comment system to use. The comment system that 9GAG is currently using really catches my eye. Take a look at this random post as an example: Despite searching through their source code ...

Template with a modal overlay

After clicking a button, I want a modal window to appear. However, if the button is inside a template-generated page, the modal window does not appear. <script type="x-template" id="company-template"> <button class="ui but ...

Unlocking two features with just a single tap

I am currently working on a website for a kiosk, where the site transitions like a photoslide between each section. To achieve this effect, I have added a layover/mask on the initial page. The layover/mask is removed using a mouse click function. This is ...

Utilizing Multiple PHP isset Statements on a Single Page

My goal is to implement multiple paginations on a single page. The pagination for all the main results is functional. However, I also want to include additional options such as Newest, Reputation, and Oldest. In the screenshot below, you can see th ...

Change the class of the div when the first div is selected

My goal is to switch the class of the #klapp element (from .klapp to .klappe) whenever a click event happens inside the #label-it container, including when the #klapp element itself is clicked. The challenge is that I am not able to use unique IDs for each ...

Tips for managing page navigation and incoming links on a site that uses only ajax technology

Currently, I am in the process of developing a fully ajax-based website which presents a challenge - the inability to provide direct page links. To address this, I have implemented a functionality where the page name gets appended to the URL each time an a ...

Executing a series of AJAX requests within a loop

I am looking for a way to optimize sending multiple AJAX calls simultaneously in Javascript/Angular. Despite my efforts to find a solution, I have come up empty-handed. My goal is to send all requests as quickly as possible without waiting for each one to ...

What are the steps to ensure that CSS3 animations function properly in Outlook 2013?

Is it possible to incorporate CSS3 animations into an email signature in Outlook 2013? I've tried adding them, but they don't seem to be working. Can anyone provide guidance on how to make CSS3 animations function properly in Outlook 2013? ...

Achieve a fading effect on an element as you scroll down the page

I successfully implemented a toggle audio button on my website, but now I would like it to fade in along with the scroll-to-top button that I also have (which operates similarly to the buttons on scrolltotop.com). Is there a simple way to achieve this? He ...

Updating JSON/Array values with jQuery Select changes

My goal is to parse through JSON data in order to achieve the following: Select an Option > List each Type e.g. Activity/Adventure, Beach Escape, City Break. On Select Change > List each Type's Value along with the Country assigned to this Op ...

Encountered an issue when attempting to save data to the database using an AJAX request in Symfony 3

i need to input text in a form and store it in my database as well as display it on my view using jQuery. here is the input form: <div class="panel rounded shadow"> <form method="post"> <textarea id="txt" class="form-control in ...

Error encountered during W3 CSS validation: Parsing error

Upon attempting to validate the code snippet below: .cd-stretchy-nav ul a { (line 186) position: relative; display: block; height: 50px; line-height: 50px; padding: 0 calc(1em + 60px) 0 1em; color: white; opacity: 1; fon ...

Maintain the previous droppable positioning after refreshing the page

I am encountering an issue with the .droppable event. I have set up two sections where I can move elements, but every time the page is refreshed, the positioning of the elements reverts to the initial position. How can I maintain the last positioning of th ...

Tips for removing a checkbox and customizing a label's style when the label wraps around the checkbox input

Hello, I'm an advanced beginner so I appreciate your patience:) I've had success removing checkboxes and styling their labels when the for="" attribute is present. However, I'm facing a challenge with a form where the labels wrap the input ...

The Importance of Including Spaces in jQuery Validation Regular Expressions

My main goal is to validate a form, and in one of the fields, I am specifically looking to allow spaces: Here is the code snippet I have been using: $.validator.addMethod( "legalname", function(value, element) { return this.optional(eleme ...

Unable to Load CSS in New Magento Template

I have reached out to the template creator's website and Magento support for assistance, unfortunately, I have not received any response so far. Despite installing a custom theme that is said to be compatible with the latest version of Magento, the C ...

Combining filters with AJAX, PHP, and MySQL for seamless interaction

I've been working on a table that displays a list of users, and I'm trying to implement filters using select boxes to refine the results based on certain parameters. The table is generated dynamically through a PHP script triggered by AJAX when t ...

What is the best way to save an AJAX success variable as a variable that is accessible outside of

After using AJAX to retrieve data as myPubscore, I encountered an issue when trying to pass myPubscore to another js file. While myPubscore printed correctly in Ajax, I faced a "ReferenceError" when attempting to print it just before sendResponse. How can ...

Contrast two sets of JSON data arrays

There are two JSON objects, one containing an array of IDs and the other containing arrays with ID and title pairs. Here is how the data looks: First JSON Data : json1 : [ "123", "456", "789" ] Second JSON Data : json2 : [ ...