getaway
getaway

Reputation: 8990

the id of the element won't change in jquery?

i.e.

$("#savenew").live('click', function(e)  {
               var user=<?php echo $user?>;
                 $.ajax({
                type: "POST",
                url: "actions/sub.php",
                data:{user: user} ,
                success: function(){
                  $('#savenew').html('<span>Unsubscribe</span>');
    $(this).attr('id', 'clean'); // this my problem my problem
                        }
            });
     }); 

the id after the ajax request, is not changing from savenew to clean, but the html is perfectly fine, so i know the ajax request is working. what do u thiunk the problem is?

Upvotes: 0

Views: 120

Answers (2)

Nick Craver
Nick Craver

Reputation: 630559

You just need to save the context (via the context option for $.ajax()) so this still refers to #savenew, like this:

$("#savenew").live('click', function(e)  {
  var user=<?php echo $user?>;
  $.ajax({
    context: this, //add this!
    type: "POST",
    url: "actions/sub.php",
    data:{user: user} ,
    success: function(){
      $(this).attr('id', 'clean').html('<span>Unsubscribe</span>');
    }
  });
}); 

Also note that this allows you to chain and clean things up a bit.

Upvotes: 2

cherouvim
cherouvim

Reputation: 31903

$(this) inside success: function(){ does not refer to $('#savenew').

As you do in the line above, you need to reference it by id:

$('#savenew').attr('id', 'clean');

Upvotes: 1

Related Questions