Yilmazerhakan
Yilmazerhakan

Reputation: 1855

Change class and text on jQuery AJAX success

I have a link:

<a href="#" class="confirmed" data-ev="1">
  <i class="icon-pencil"></i>
  <span class="text-warning">confirm this!</span>
</a>

I tried everything in comment sections in this code:

$(".confirmed").on("click", function(e){
   e.preventDefault();
   var that  = this;
   $.ajax({
    url: '/confirmed/',
    type: 'POST',
    data: {
      event_type: $(that).data('ev')
    },
        success: function(result) {
          $(that).data('ev',"10");
          // here I want to change the class of i tag to "icon-ok"
          // and span.class to "text-success"
          // and span.text to "confirmed"
        }
  });
});

Upvotes: 1

Views: 3153

Answers (2)

Josh
Josh

Reputation: 44906

You've got the parent <a> element already. You just need to do a find:

$(that).find('i').addClass('icon-ok');
$(that).find('span').text('blah');

Upvotes: 1

Robert Fricke
Robert Fricke

Reputation: 3643

Add class with jQuery.addClass()

Remove class with jQuery.removeClass()

Set text content with jQuery.text()

This are very simple jQuery tasks, and you would easily have found them using Google. That is also why my post does not contain any code.

Upvotes: 0

Related Questions