var id = $(this).children().html(); // id is 5 $.ajax({ url: \'ajax.php?id=\' + id, success: function(data) { id = data; // id is 1 } }); if(id == 1){
your if statement executes before your .ajax call completes