Alright, so I have a button that when clicked, will change the class of a given div. The thing is, I only want this to happen if a variable (answer) does not contain the string “No”. This works… but the problem is that even if the variable doesn’t contain the word “No”, the code won’t execute. The ajax result contains either “No User ID” or “User ID:#”.
$(document).ready(function() {
$('.like').on('click', function() {
postID = $(this).attr('id').replace('like_', '');
// Declare variables
value = '1';
myajax();
return false;
var doit = answer.IndexOf("No");
if (doit < 0){
$('#post_' + postID).removeClass('dislike').addClass('like');
}
});
function myajax(){
// Send values to database
$.ajax({
url: 'check.php',
//check.php receives the values sent to it and stores them in the database
type: 'POST',
data: 'postID=' + postID + '&value=' + value,
success: function(result) {
answer = result;
$('#Message_' + postID).html('').html(result).prependTo('#post_' + postID);
}
});
}
});
SO I know my problem lies with the var doit. I just can’t figure out what’s wrong with it.
the
successfunction is executing after the call tomyajaxreturnsthe first ‘a’ in ajax stands for asynchronous, so you want to move the entire block of code into the success function, likeso: