Somename Somename - 3 months ago 9
Ajax Question

Whats the error in this AJAX jQuery?

I'm trying to

removeClass
and
addClass
on the jQuery
$.get
success
. But it doesn't seem to work with
$.get
and works only if i include the entire
$.ajax
which i dont want to do.

The following code works:

$(".result").on("click", ".myul li", function(){
var mydiv = $(this).data("id");
var enumgroup = {};
enumgroup["id"]=mydiv;

$.ajax({
type: 'POST',
url: 'checkenumresult.php',
context:this,
data: {id: mydiv},
success: function (data) {
$(this).removeClass("forseen0").addClass("forseen1");
}
});
});


The following code doesnt work:

$(".result").on("click", ".myul li", function(){
var mydiv = $(this).data("id");
var enumgroup = {};
enumgroup["id"]=mydiv;

$.get("checkenumresult.php", enumgroup, function(){
context:this, //tried without this too.
success: function(data){
$(this).removeClass("forseen0").addClass("forseen1");
}
});
});


Both the codes are working with respect to posting data to the php page. Only the 2nd code is not changing class of the element on
success.
What is wrong with the second code?

Answer
$(".result").on("click", ".myul li",  function(){
    var mydiv = $(this).data("id");
    var enumgroup = {};
    enumgroup["id"]=mydiv;    
    $.get("checkenumresult.php", enumgroup, function(data){
       $(mydiv).removeClass("forseen0").addClass("forseen1");        
    });
});

the third argument in the above get call is the callback for the response

Comments