I need to use jQuery to add a div with the class 'address-line' to another element with the id 'add-results', and then add several elements to the newly created div. Then repeat this for each element of the object.
Here is what I am trying to summarize:
<div id="add-results"> <div class="address-line"> <h2>title 1</h2> <p>desc 1</p> <img src="thing1.png" /> </div> <div class="address-line"> <h2>title 2</h2> <p>desc 2</p> <img src="thing2.png" /> </div> </div>
Here is one of many things I've tried (chdata is an object, chvalue will store data):
$.each(chdata, function(name, chvalue) { $('#add-results').append('<div class="address-line">'); $('#add-results').append('<h2>'+chvalue['title']+'</h2>');
.. but, of course, this leads to the following:
<div id="add-results"> <div class="address-line"></div> <h2>title 1</h2> <p>desc 1</p> <img src="thing1.png" /> <div class="address-line"></div> <h2>title 2</h2> <p>desc 2</p> <img src="thing2.png" /> </div>
I also tried using:
$('#add-results').append('<div class="address-line">') .append('<h2>'+chvalue['title']+'</h2>');
.. but the same result. How can I achieve this?
source share