0

I have a page structured with jquery mobile. If i populate a list with a static code:

    <script>
document.write('<ul data-role="listview">');


document.write('<li data-icon="false"><a href="#" id="mortadella"><img src="images/app/prod.jpg"><h2>Sisa</h2><p class="wrap">mortadella affettatagr.120</p><span class="ui-li-count">2,70 €</span></a></li>');
document.write('<li data-icon="false"><a href="#" id="mortadella"><img src="images/app/prod.jpg"><h2>Sisa</h2><p class="wrap">mortadella affettatagr.120</p><span class="ui-li-count">2,70 €</span></a></li>');             



document.write('</ul>');

</script>

i get this result: image

Now, i try to do it dinamically, reading from a database with ajax and json. This is the code:

<script>
document.write('<ul data-role="listview">');
    $.ajax({
        url: 'db_to_app_prod.php',
        dataType: 'jsonp',
        jsonp: 'jsoncallback',
        timeout: 5000,
        success: function(data, status){
            $.each(data, function(i,item){ 

document.write('<li data-icon="false"><a href="#" id="mortadella"><img src="images/app/prod.jpg"><h2>Sisa</h2><p class="wrap">mortadella affettatagr.120</p><span class="ui-li-count">2,70 €</span></a></li>');

            }); 

        },
        error: function(){
           output.text('There was an error loading the data.');
        }
    });


document.write('</ul>');
</script>

And this is the result: image As you can see, the layout is totally broken now. What happens? Why? How do i fix this to obtain dinamically the first result?

EDIT: This is another try i did:

$(document).ready(function(){
    $(document).bind('deviceready', function(){
        //Phonegap ready
        onDeviceReady();
    });


    //var output = document.getElementById("output");
    var _ul = document.createElement('ul');

    _ul.setAttribute("data-role", "listview");


    $.ajax({
        url: 'db_to_app_prod.php',
        dataType: 'jsonp',
        jsonp: 'jsoncallback',
        timeout: 5000,
        success: function(data, status){
            $.each(data, function(i,item){ 

                var _li =  document.createElement('li');
                _li.setAttribute("data-icon", "false");
                _li.innerHTML = '<li data-icon="false">'+
                '<a href="" id="'+item.id+'">'+
                '<img src="http://gestisciapp.it/gruppodipalo/images/'+item.img+'">'+
                '<h2>'+item.marca+'</h2>'+
                '<p class="wrap">'+item.descrizione+'</p>'+
                '<span class="ui-li-count">'+item.prezzo+' €</span>'+
                '</a></li>';    

                _ul.appendChild(_li);

            }); 

        },
        error: function(){
           output.text('There was an error loading the data.');
        }
    });
    document.getElementById("output").appendChild(_ul);

});
Tony33
  • 147
  • 1
  • 13
  • @AlexKudryashev i have removed the duplicate `
  • ` , it's still the same. About mixing jquery and DOM, do you have a better solution? I need to read from database and display results generating html. How can achieve this in other way than the one i used?
  • – Tony33 Jun 12 '16 at 15:36