0

I have an array of JSON object like this :

var array = [
{title : "TEST1", img : "http://www.myTest1.com/test1.jpg"},
{title : "TEST2", img : "http://www.myTest2.com/test2.jpg"},
{title : "TEST3", img : "http://www.myTest3.com/test3.jpg"}
]

I would like to create the following bloc element (using the previous array) and add this bloc into an existing <div id="content"></div> in my webpage :

<div id="line1">

        <div class="item">
            <h2 id="item_text">
                <span>TEST1</span>
            </h2>
            <div class="img-div">
                <img id="item_img" src="http://www.myTest1.com/test1.jpg" alt="" height="333px" width="500px" />
            </div>
        </div>

        <div class="item">
            <h2 id="item_text">
               <span>TEST2</span>
            </h2>
            <div class="img-div">
                 <img id="item_img" src="http://www.myTest2.com/test2.jpg" alt="" height="333px" width="500px" />
            </div>
        </div>

        <div class="item">
            <h2 id="item_text">
               <span>TEST3</span>
            </h2>
            <div class="img-div">
                <img id="item_img" src="http://www.myTest3.com/test3.jpg" alt="" height="333px" width="500px" />
            </div>
        </div>

</div>

How can i do that in Javascript / JQuery ?

wawanopoulos
  • 9,614
  • 31
  • 111
  • 166

6 Answers6

1

Try this FIDDLE

Create a model which stores the HTML content to be populated. Iterate over the array and append it.

 var line = $("#line1");
 for (a in array) {
     appendModel(a);
 }

 function appendModel(num) {
     var model = '<div class="item"><h2 id="item_text"><span>' + array[num].title + 
         '</span></h2><div class="img-div"><img id="item_img" src="' + array[num].img + '
     " alt="" height="333px" width="500px" /></div></div>';
     line.append(model);
 }
yashhy
  • 2,856
  • 5
  • 31
  • 57
0

This what you are looking for?

var myNewDiv = $("<div></div>");

//append content to myNewD

myNewDiv.appendTo($("#content"))
Karl-André Gagnon
  • 33,662
  • 5
  • 50
  • 75
Joakim M
  • 1,793
  • 2
  • 14
  • 29
0
document.getElementById("line1").innerHTML += "<div id='content'>";

for(var i = 0; i < 3; i++) {
document.getElementById("line1").innerHTML += "<h1>" + array[i].title + "</h1>"
document.getElementById("line1").innerHTML += "<img src='" + array[i].img + "'>";
}

document.getElementById("line1").innerHTML += "</div>";

Fiddle. Hope that helps.

chris97ong
  • 6,870
  • 7
  • 32
  • 52
0

There is a ; in your array its supposed to be a ,

Use this:

var array = [
{title : "TEST1", img : "http://www.myTest1.com/test1.jpg"},
{title : "TEST2", img : "http://www.myTest2.com/test2.jpg"},
{title : "TEST3", img : "http://www.myTest3.com/test3.jpg"}
]

Then you can do something like this:

var arrayLength = array.length;
var insertHTML = "";
for (var i = 0; i < arrayLength; i++) {
  insertHTML += array[i].title + array[i].img;
}

document.getElementById("youdivID").innerHTML= insertHTML;
Mike
  • 62
  • 6
0

You can use jQuery.each() to create the html and jQuery.append() function to append it to the target div

var array = [
    {title : "TEST1", img : "http://www.myTest1.com/test1.jpg"},
    {title : "TEST2", img : "http://www.myTest2.com/test2.jpg"},
    {title : "TEST3", img : "http://www.myTest3.com/test3.jpg"}
];

/*
 * put your target div selector here
 * I have simply played with this page elements
 */
var newHtml = '';

    $(array).each(function(index,item){
    newHtml += '<div class="item"><h2 id="'+ item.title +'"><span>'+ item.title +'</span></h2><div class="img-div"><img id="'+ item.img +'" src="'+ item.img +'" alt="" height="333px" width="500px" /></div></div>';
});

$target = $('#question-header').append(newHtml);
Alberto De Caro
  • 5,147
  • 9
  • 47
  • 73
  • Change your loop function parameter positions : `array.forEach(function(item,index){ }` – Zword Apr 02 '14 at 12:49
0

try :

var array = [
        {title : "TEST1", img : "http://www.myTest1.com/test1.jpg"},
        {title : "TEST2", img : "http://www.myTest2.com/test2.jpg"},
        {title : "TEST3", img : "http://www.myTest3.com/test3.jpg"}
        ]
        function gen(myArray){
            var html = '<div id="line1">'
            for (var i = 0, i < myArray.length; i++){
                html += '<div class="item"><h2 id="item_text"><span>'+myArray[i].title+'</span></h2><div class="img-div"><img id="item_img" src="'+myArray[i].img+'" alt="" height="333px" width="500px" /></div></div>';
            }
            html += '</div>';
            return html;
        }

        var HTML = gen(array);
        $('#content').html(HTML);
Gwenc37
  • 2,064
  • 7
  • 18
  • 22