Prashant16
Prashant16

Reputation: 1526

Display Message on success Using Jquery

On the success function of jquery ajax request i am displaying a message in span tag.

success: function(data) {
       if(data = true) {
          $("#result").text("is available"); 
       }else {
          $("#result").text("is not available"); 
       }
});

When data is true then "is available is display which is ok but when data is false then still it display "is available instead of "is not available.

Upvotes: 0

Views: 1109

Answers (3)

Mooseman
Mooseman

Reputation: 18891

data = true needs to be data === 'true'.

data = true attempts to set the value of data to the Boolean true. data === 'true' compares data to the string 'true'.


For more info on JS comparison operators, see this page.

Upvotes: 1

user3802905
user3802905

Reputation: 27

You're assigning data to true, do

data == true

Upvotes: 1

caspian
caspian

Reputation: 1804

You just want to test if data exists. Use the following

success: function(data) {
       if(data) {
          $("#result").text("is available"); 
       }else {
          $("#result").text("is not available"); 
       }
});

Upvotes: 0

Related Questions