user1022585
user1022585

Reputation: 13651

jQuery change TD color

I'm trying to change the color of a TD cell when I click on it. First I needs to check data on page.php though:

$(".change").click(function(event) {
    var section = $(this).data('id');
    $.post("page.php", { td: change }, function(data){
        $(this).css('background', '#000'); <------- THIS
        });
    });

How can I make the this line work? I understand I need to set this to something, but how?

Upvotes: 0

Views: 343

Answers (1)

Gazler
Gazler

Reputation: 84150

The scope (context) of this is changed in the function callback.

Something like this should work:

$(".change").click(function(event) {
    var self = this;
    var section = $(this).data('id');
    $.post("page.php", { td: change }, function(data){
        $(self).css('background', '#000'); <------- THIS
    });
});

Upvotes: 2

Related Questions