Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with removing element

Tags:

jquery

I don't know why this working:

 $('.deleteQuestion').live('click', function(){
        $.ajax({
            type: 'GET',
            url: '/delete_question/' + $(this).attr('name') + '/',
            success: $('[what="question"][name="' + $(this).attr('name') + '"]').remove()                            
        });      
    });

but this not working:

 $('.deleteQuestion').live('click', function(){
        $.ajax({
            type: 'GET',
            url: '/delete_question/' + $(this).attr('name') + '/',
            success: function(){$('[what="question"][name="' + $(this).attr('name') + '"]').remove();}                            }
        });      
    });

Does someone know?

like image 612
Cadilac Avatar asked Jan 18 '26 20:01

Cadilac


1 Answers

The success callback doesn't operate on the same this that the click handler does. Save it in a variable:

$('.deleteQuestion').live('click', function(){
    var element = $(this);
    $.ajax({
         type: 'GET',
         url: '/delete_question/' + $(this).attr('name') + '/',
         success: function(){ //this has to be a function, not a jQuery chain.
              $('[what="question"][name="' + element.attr('name') + '"]').remove();}
         }
    });      
});
like image 158
Dennis Avatar answered Jan 20 '26 11:01

Dennis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!