HELPME
HELPME

Reputation: 754

How to replace ajax response text with original text?

I get the ajax response, which is replacing the original text if the checkbox is checked. But how to do the opposite? Replace the response with original text if the user has unclicked the checkbox?

$.ajax({
   type: 'POST',
   url: 'index.php?r=recommendations/entry/text',
   success: function(response) {
       if (checkbox.is(":checked")) {
           recommendationText.html(response);
       }
   },
});

Upvotes: 0

Views: 59

Answers (1)

Sébastien Gicquel
Sébastien Gicquel

Reputation: 4386

Is you original text always the same ?

In that case, a simple if else could do the job :

You can execute a different statement in case the original if expression evaluates to FALSE

$.ajax({
   type: 'POST',
   url: 'index.php?r=recommendations/entry/text',
   success: function(response) {
       if (checkbox.is(":checked")) {
           recommendationText.html(response);
       } else { 
          recommendationText.html("Lorem ipsum"); 
       }
   },
});

Otherwise, you can store your original text in a variable :

var originalText;
originalText  = $("#your_element").text()

You can use you variable like this :

else { 
      recommendationText.html(originalText); 
}

Upvotes: 1

Related Questions