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){
Refactoring the code like this would work:
function by_id() {
if(id == 1) {
... do something ...
}
}
var id = $(this).children().html();
$.ajax({
url: 'ajax.php?id=' + id,
success: function(data) {
id = data;
by_id(); // call on successful ajax load
}
});
The advantage of wrapping your id
logic in a function allows you to call it anywhere - be it at the end of an AJAX request, at page load, on a button press, etc.