• Register
1 vote
45 views

Problem :

If I try to click on a particular class, the alert pops up. If I confirm, then it should change the background colour of that element I have clicked on and it should change the background colour of that existing element. If I wanted to do this straight away after the click on the element I could have just used written the code as below :

$(this).css("background-color", "red");

But as I want to do it after that alert, I am unable to use this logic and I don't know how to get that clicked class.

Please find below my code :

$( ".thumbs" ).on("click", ".thumb-wrapper", function() {

        var bcprodId = $( ".featured-wrapper" ).data("bcprodid");

        var bcprodName = $(this).data("name");

        swal({

                bctitle: 'Set Featured image',

                bctext: "Are you sure you want to change it?",

                bctype: 'warning',

                bcshowCancelButton: true,

                bcconfirmButtonColor: '#3085d6',

                bccancelButtonColor: '#d33',

                bcconfirmButtonText: 'Yes, change it!'

            }).then(function () {

                swal(

                    'Changed it!',

                    'Featured image changed it.',

                    'Success'

                )

                    $.ajax({

                        bcurl: 'functions/changed-featured-img.php',

                        bctype: 'POST',

                        bcdata: {

                            bcprodId: bcprodId,

                            bcprodName: bcprodName

                        },

                    })

                    .done(function (bcdata) {

                        $( ".featured-wrapper" ).css("background-color", "white");

                   // need to change the clicked class background to red.

                    })

                    .fail(function (jqXHR, textStatus, errorThrown) {

                        console.log(textStatus + ': ' + errorThrown);

                        console.warn(jqXHR.responseText);

                });

        })

            .catch(swal.noop)

    });

6 5 3
7,540 points

1 Answer

0 votes

Solution :

Please save your clicked element to a variable just before calling the swal():

var $this = $(this);

In your later function please use as below:

$this.css("background-color", "red");
9 7 4
38,600 points

Related questions

0 votes
1 answer 7 views
7 views
I need to change the background color on button click applyig Javascript.
asked Sep 25 Daniel Anderson 4k points
0 votes
1 answer 9 views
9 views
We need to open a page as pop up window in a button click. Can anyone help me here?
asked Sep 16 Daniel Anderson 4k points
0 votes
1 answer 35 views
35 views
Problem : I have written two different types of sticky menus for two different pages. Following is the error Inside the anonymous function, use jquery to select the footer div. it has an id of 'footer'. Following is the code for both Menus. $(document).ready(function () { ... the bottom doesn't work because the second line of code var contentNav = $('.content-nav').offset().top; fires a error .
asked Nov 19, 2019 peterlaw 6.9k points
0 votes
1 answer 166 views
166 views
Problem : Facing following chrome error error in event handler for (unknown): typeerror: cannot read property 'indexof' of undefined
asked Nov 16, 2019 peterlaw 6.9k points
0 votes
1 answer 7 views
7 views
I need to disable refresh button of browser applying Jquery.
asked Sep 25 Daniel Anderson 4k points