I use the following code to add dynamically HTML string to a div element. I would like to change the image every second after added HTML to the DOM. But my code doesn't work. Where is my mistake?
var images = ["http://pathToImg1.jpg", "http://pathToImg2.jpg"];
var result = '<div class="result">'+
'<a href="'+url+'" target="_blank" id="resultTitle" class="resultTitle">'+urlTitle+'</a>'+
'<p id="resultDescription" style="height: 15px;">'+
'<div class="resultImg" style="background-image: url('+images[0]+');"></div>'+
'<span class="resultDescription" style="margin:0px 15px 0px 0;">'+description+'</span></p>'+
'<p id="resultUrl" class="resultUrl">'+url+'</p>'+
'</div>';
$("#"+targetId).append(result);
var i = 0;
setInterval(function() {
var el = $(result).find(".resultImg");
$(el).css('background-image', 'url("' + images[i] + '")');
i = i + 1;
if (i == images.length) {
i = 0;
}
}, 1000);
ivariable before setting the background also try to change$(result).find(".resultImg")with$("#"+targetId).find(".resultImg")url,urlTitleanddescriptionvariables that you are using in your template?