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){
The AJAX function is asynchronous. It will run in the background and when it is done getting the ajax.php page it will run the success function.