Skip to content Skip to sidebar Skip to footer

Items.push Variable Using Javascript

I have an JSON array, that i manipulate inside my app, ... $.each(data, function(key, val) { val = link + val; foto = val; foto = foto.substr(0, foto.lastIndexOf('.'))

Solution 1:

Exploiting jQuery further, you might want to try something like this :

...
var $ul = $("<ul/>");//jQuery object containing a dummy UL element in which to accumulate LI elements.
$.each(data, function(key, val) {
    var url = link + val;
    var foto = url.substr(0, url.lastIndexOf(".")) + ".jpg";
    var $a = $('<a/>').attr('href',url).append($("<img/>").attr('src',foto)).on('click', function() {
        cordova.exec("ChildBrowserCommand.showWebPage", $(this).attr('href'));
        returnfalse;
    });
    $ul.append($('<li/>').attr('id',key).append($a));
});
$("#archivio-num").html($a);
...

Here, instead of accumulating the HTML in an array, actual LI elements are accumulated in a jQuery-wrapped UL element, which is available for further treatment (eg. insertion into the DOM) later in the code.

Post a Comment for "Items.push Variable Using Javascript"