function genTask(elem){ elem.each(function(){ $this=$(this).parent(\'.cntTasks\'); var pattern=/taskId-(.*)$/ var idTask=$this.attr(\
In your $.ajax call add async: false and it will send a blocking request.
$.ajax
async: false