vendredi 13 février 2015

JQuery wrap() function only correctly wraps links around image first time through a loop


The following code is simply supposed to loop through a list of artists generating an image and a link for each item. This works the first time through the loop, however for subsequent iterations the wrap() link around the image doesn't seem to be added. I've tried a few various ways, including having all the code to be wrapped in a separate variable and the best I've got is that the link will be generated, but not actually wrapped around the thumb class (*). I'm afraid client-side goings-on aren't my strong point so I'm struggling to see why only the first iteration of the loop is successfully wrapped in a link.


*That was using dynamically-generated attribute IDs and wrapping that instead of using the class attribute as follows.



function showArtists(adata) {
$("#artists").html("");

for (var i = 0; i < adata.artists.length; i++) {

var artist = adata.artists[i];

var artistSpan = document.createElement("span");
artistSpan.setAttribute("class", "artistView");

var artistPic = document.createElement("img");
artistPic.setAttribute("class","thumb");
artistPic.setAttribute("src",artistPicture)

$('.thumb').wrap(function() {return "<a href='#' onclick ='getAlbums(artist)'> </a>"});

artistSpan.appendChild(artistPic);

$("#artists").append(artistSpan);
}

}




Aucun commentaire:

Enregistrer un commentaire