Giuseppe De Luca Giuseppe De Luca - 13 days ago 6
jQuery Question

Get data-attributes values and append() them in a string

I want to retrieve data-attributes values and append them into a string.

So far, I've got the retrieving part to work but as jquery & programming are not really my thing, all I could manage is to show the result in an alert.

<a data-1="312UaZo2kvL" data-2="41qmDp9xesL" data-3="41pJL%2BWA-eL">Link</a>

var imaglist = "";
$.each($('a').data(), function(i, v) {


var temp = '<img src ="http://ecx.images-amazon.com/images/I/' + v + '.jpg"></li>';
var strToInsertInimaglist = '<li data-thumb ="http://ecx.images-amazon.com/images/I/' + v + '._SL75_.jpg">' + temp;

alert(strToInsertInimaglist);


I can't figure out how to insert or append the results in the imaglist variable that I can use later (I want to dynamically update a slider actually)

Would somebody be so kind as to take a look and tell me how I can do that please ?

Thanks!

https://jsfiddle.net/Pidl/s2Lkd4wv/

Answer

imaglist is outside the each so it should be scoped right. Did you try appending to it? imaglist += strToInsertInimaglist; ?

Comments