Anthony Hung
Anthony Hung

Reputation: 43

how can I change content in span after ajax success in MVC 4

<div>
  <span class="label">1</span>
  <a href="#" class="click">up</a>
</div>

Javascript

$('.click').click(function(){
//$(this).parent().find('.label').html(2);
$.ajax({
    ....
    success: function(result){
    $(this).parent().find('.label').html(2);
    }
});

});

if i don't use ajax.post, value in change.. and when i use, it doesn't change.

I don't know what happen? and how can i fix it.

Give me some advices please.

Upvotes: 1

Views: 887

Answers (1)

user3559349
user3559349

Reputation:

$(this) is not referring to your link (its inside the $.ajax() function). Assign the label element to a javascript variable before you make the ajax call so it can be accessed inside the ajax function.

$('.click').click(function(){
  var label = $(this).parent().find('.label');
  // or $(this).prev('.label');
  $.ajax({
    ....
    success: function(result){
      label.html(2);
   }
});

Upvotes: 1

Related Questions