Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
76
rated 0 times [  81] [ 5]  / answers: 1 / hits: 20863  / 12 Years ago, tue, december 4, 2012, 12:00:00

I have an array as a attribute on a link.



Here is the array



images=[one.jpg,two.jpg]


How would I parse through this array and have it read back to me one.jpg,two.jpg?



This is what I am doing now and it is giving me an error back. I don't believe json parsing is whats needed here.



var imgs = $(#+number).attr(images);
var imgList = jQuery.parseJSON(imgs);


EDIT: ACTUAL CODE



var number = $(this).attr(data-id);

var url = $(#+number).attr(url);
$(.portfolio-url).html(<h3 class='pacifico'>url</h3><p><a href='http://+url+' target='_blank'>+url+</a></p>);

var cli = $(#+number).attr(client);
$(.portfolio-client).html(<h3 class='pacifico'>client</h3><p>+cli+</p>);

var pgs = $(#+number).attr(pages);
pgs = pgs.replace(/[/g,);
pgs = pgs.replace(/]/g,);
pgs = pgs.replace(//g,);
var pages = new Array();
pages = pgs.split(,);

var img = $(#+number).attr(images);
img = img.replace(/{/g,);
img = img.replace(/}/g,);
img = img.replace(//g,);
var images = new Array();
images = img.split(,);

var portSkills = <h3 class='pacifico'>skills</h2>;
portSkills += <p>;
for (i=0;i<pages.length;i++) {
if (pages[i] != Clients) {
var finalPage = ;
for (j=0;j<pages[i].length;j++)
{
var ch = pages[i].charAt(j);
if (ch == ch.toUpperCase()) {
finalPage += ;
}
finalPage += pages[i].charAt(j);
}
portSkills += finalPage+<br />;
}
}
portSkills += </p>;
$(.portfolio-skills).html(portSkills);

var imgs = $(#+number).attr(images);
var imgList = jQuery.parseJSON(imgs);


Basically, its looping through parameters


More From » jquery

 Answers
22

I'd encourage you to modify your attribute-value format to something along these lines:



<div id=one data-images=file1.jpg,file2.jpg>Foo, Bar</div>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​


Note here I'm using a valid data- attribute, and the value of this attribute is just a list of comma-separated filenames. No need to place [ or ] in this value in order to get an array.



Now, to get your array:



var images = $(#one).data(images).split(,);


Which results in the following array:



[file1.jpg, file2.jpg]

[#81626] Monday, December 3, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
payton

Total Points: 307
Total Questions: 96
Total Answers: 83

Location: Saint Vincent and the Grenadines
Member since Sat, Sep 11, 2021
3 Years ago
;