I have the following function:
function displayResults(Items) {
$("#result").text("");
$("#result").append('<div class="car-offers">');
$("#result").append('<div class="purple"></div>');
$("#result").append('<img src="images/caroffer.jpg" alt="" title="" width="213" height="117" />');
$("#result").append('<h3>titleeee</h3>'); // ' + Items[i].Title + '
$("#result").append('<span>Year: 2003</span>');
$("#result").append('<span>Milage: 172,000 Km</span>');
$("#result").append('<span class="price">53,000 QR</span>');
$("#result").append('<a href="">Link</a>');
$("#result").append('</div>');
$("#result").append('<div class="car-offers">');
$("#result").append('<div class="purple"></div>');
$("#result").append('<img src="images/caroffer.jpg" alt="" title="" width="213" height="117" />');
$("#result").append('<h3>titlee22</h3>'); // ' + Items[i].Title + '
$("#result").append('<span>Year: 2003</span>');
$("#result").append('<span>Milage: 172,000 Km</span>');
$("#result").append('<span class="price">53,000 QR</span>');
$("#result").append('<a href="">Link</a>');
$("#result").append('</div>');
}
my problem is that at run-time the html is being displayed like: <div class="car-offers"></div>
so all the page is being messed up
You cannot append incomplete fragments of HTML with .append()
. Unlike document.write
, jQuery's .append()
method parses the passed string into elements before appending them to the DOM.
So when you do:
$("#result").append('<div class="car-offers">');
jQuery parses the given string into a div
element and assigns the car-offers
value to its className
property, then appends the newly created element to the #result
element.
Appending the whole HTML string in a single operation will fix that, so jQuery knows how to parse the given string correctly.
Personally, I wouldn't suggest putting that much HTML inside of a JS file. You can consider putting that inside of a div
with display:none
then simply call .show()
on it. Or have it initially in the page, .detach()
it storing in a variable and .append()
it back when necessary.
You can use a array with join to solve this
function displayResults(Items) {
$("#result").text("");
var array = [];
array.push('<div class="car-offers">');
array.push('<div class="purple"></div>');
array
.push('<img src="images/caroffer.jpg" alt="" title="" width="213" height="117" />');
array.push('<h3>titleeee</h3>'); // ' + Items[i].Title + '
array.push('<span>Year: 2003</span>');
array.push('<span>Milage: 172,000 Km</span>');
array.push('<span class="price">53,000 QR</span>');
array.push('<a href="">Link</a>');
array.push('</div>');
array.push('<div class="car-offers">');
array.push('<div class="purple"></div>');
array
.push('<img src="images/caroffer.jpg" alt="" title="" width="213" height="117" />');
array.push('<h3>titlee22</h3>'); // ' + Items[i].Title + '
array.push('<span>Year: 2003</span>');
array.push('<span>Milage: 172,000 Km</span>');
array.push('<span class="price">53,000 QR</span>');
array.push('<a href="">Link</a>');
array.push('</div>');
$("#result").text(array.join(''));
}
This is an issue I've just come across too. An option is to create the div containers first in a loop then populate them after as you require:
for(var i = 0; i < 12; i++){
$(el).append('<li class="scene-block"></li>'); // create container li tags
//fill empty li tags with content
for(var j = 0; j < 2; j++){
$(el).find('li').last().append('<div class="select-a-scene-bg"></div>');
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With