What I try to achieve. On a change event html is changing. Dependent on the value of a select, pieces of html need to loaded as much time as the value. Within those pieces of html I need to replace some variables with the var i of my for-loop and some other vars. (for back-end shizzle). The final html need to be append at a div.
The problem is that when I try to access the variables in the $.get() or $.load() function, it always take te latest variable. When I access the variables in the append function like this, everything goes as it should go
$('.reizigers-verzekeringen').append('<p>'+title+'</p>);
example of what is loaded and need replacement be replace (between **):
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title"><a data-toggle="collapse" data-parent="#accordion" href="#collapseinsurance*i*" class=""> Verzekeringen *TITLE* <i class="fa fa-plus pull-right"></i></a></h4>
</div>
</div>
The full code...
$(".aantal-reizigers").change(function(){
$(".reizigers-data").empty();
$(".reizigers-verzekeringen").empty();
var aantalReizigers = $(".aantal-reizigers").val();;
var title="";
var openclosedata="";
var opencloseverzekeringen="";
for (var i = 1; i <= aantalReizigers; i++) {
if(i == 1){
title = "hoofdreiziger";
openclosedata='<div id="collapse'+i+'" class="panel-collapse in" style="height: auto;">';
opencloseverzekeringen='<div id="collapseinsurance'+i+'" class="panel-collapse in" style="height: auto;">';
}
else{
title = "reiziger "+i;
openclosedata='<div id="collapse'+i+'" class="panel-collapse collapse">';
opencloseverzekeringen='<div id="collapseinsurance'+i+'" class="panel-collapse collapse">';
}
$.get("booking/verzekeringen.html", function(html){
html.replace("*TITLE*", title);<!--title is always the latest-->
})
$(".reizigers-verzekeringen").append(
'<p>'+title+'</p>'<!--this is going fine-->
);
};
});