Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Value of $(this) changes with confirm

Tags:

jquery

I'm wondering why $(this) is not working the way I'm expecting it to? In the code below, nothing happens when you click'Remove Image.' If you comment out the confirm statement then the background changes to green when you click 'Remove Image'. Do you know why this is? It seems like $(this) is pointing to something else because of the confirm statement. Thanks in advance!

<a href="#" class='thumbnail the-one delete-file'><i class="icon-remove"></i>Remove Image</a>

$('.the-one').click(function(){
    if(confirm("What do you say?")) { return true;} else {return false;}
    $(this).css("background", "green");

});
like image 877
Tom Avatar asked Dec 26 '22 03:12

Tom


2 Answers

You're returning before you set the css, so that line isn't getting executed. Try:

$('.the-one').click(function(){
    if (confirm("What do you say?")) { 
        $(this).css("background", "green");
        return true;
    } else {
        return false;
    }
});
like image 130
dinjas Avatar answered Jan 13 '23 14:01

dinjas


Because you have return before it. Everything after return won't run.

like image 40
xdazz Avatar answered Jan 13 '23 15:01

xdazz