maclenin
January 19th, 2012, 12:53 PM
I am having difficulty "wrangling" output from the following....
I am creating a script which, essentially, pushes elements from a single multi-dimensional reference array, into a handful of smaller sub-arrays, to yield a series of web-based thumbnail galleries (A,B and C).
Each of the images within the thumbnail galleries has an onclick event handler to bring up a larger version of the image, ideally, on click.... However, I am having trouble configuring the onclick function, properly.
A. BACKGROUND
1. The reference array is made up of 10 elements, each of which is "tagged" ("A","B","C") to "associate" it with a unique sub-array or gallery:
var ref=new Array();
ref[0]=new Array ("image0","A");
ref[1]=new Array ("image1","A");
ref[2]=new Array ("image2","B");
ref[3]=new Array ("image3","A");
ref[4]=new Array ("image4","B");
ref[5]=new Array ("image5","C");
ref[6]=new Array ("image6","C");
ref[7]=new Array ("image7","C");
ref[8]=new Array ("image8","A");
ref[9]=new Array ("image9","A");
var rlen=ref.length;
2. Each of the sub-arrays is generated on web page load...
<body onload="create_sub_A(); create_sub_B(); create_sub_C();">
3. ...via these functions:
var=sub;
function create_sub_A() {sub="A"; create_new_array(); set_up_gallery();}
function create_sub_B() {sub="B"; create_new_array(); set_up_gallery();}
function create_sub_C() {sub="C"; create_new_array(); set_up_gallery();}
4. which each call:
function create_new_array()
{
sub_Array=new Array();
for (i=0; i<=rlen; i++)
{
x=ref[i][1];
if (sub==x)
{
sub_Array.push(i);
}
slen=sub_Array.length;
}
5. and the gallery set up function, which displays the thumbnail galleries in sub-array-defined groups:
function set_up_gallery()
{
for (i=0; i<=slen; i++)
{
id=i;
document.getElementById(sub+"_thumbnail<img>"+id).src=ref[sub_Array[i]][0];
document.getElementById(sub+"_thumbnail<a>"+id).onclick=larger_image(sub_Array[i]);
console.log(sub+sub_Array[i]);
}
thumbnail gallery A - comprises 4 images (image0,image1,image8,image9)
thumbnail gallery B - comprises 2 images (image2,image4)
thumbnail gallery C - comprises 4 images (image3,image5,image6,image7)
6. Finally, here is the "larger_image" function, which I have yet to get working properly. The problem occurs when I try to define the call to the onclick function as I show in step 5.
function larger_image(a)
{
current_image=a; document.getElementById("lg_image").src=ref[sub_Array[current_image]][0];
}
B. THE CRUX?
a is the value of sub_Array (or is it the "i" within that statement?) for each respective thumbnail - which corresponds to the original ref array index values.
For example, if I wanted to see a larger image0 - I [I]should be able to click on the first image in thumbnail gallery A, which should pass 0 from the thumbnail via onclick="larger_image(sub_Array[i])" - completing src=ref[0][0]; to bring up image0....
However, I can't get onclick defined in such a way as to allow me bring up the larger image (note: thumbnails are currently defined by height="50" in the html).
After I assign the onclick value (see step 5.) and refresh the web page, the large images cycle from first to last, automatically, as if the onclick "button" were being held down as the web page loaded....
console.log(sub+sub_Array[i]);, as defined in step 5. shows that the proper values are being generated by... sub_Array[i] ? ...
A0
A1
B2
A3
B4
C5
C6
C7
A8
A9
...they (the numbers, that is) just aren't being assigned to the onclick event handler properly....
Thanks for any wisdom.
I am creating a script which, essentially, pushes elements from a single multi-dimensional reference array, into a handful of smaller sub-arrays, to yield a series of web-based thumbnail galleries (A,B and C).
Each of the images within the thumbnail galleries has an onclick event handler to bring up a larger version of the image, ideally, on click.... However, I am having trouble configuring the onclick function, properly.
A. BACKGROUND
1. The reference array is made up of 10 elements, each of which is "tagged" ("A","B","C") to "associate" it with a unique sub-array or gallery:
var ref=new Array();
ref[0]=new Array ("image0","A");
ref[1]=new Array ("image1","A");
ref[2]=new Array ("image2","B");
ref[3]=new Array ("image3","A");
ref[4]=new Array ("image4","B");
ref[5]=new Array ("image5","C");
ref[6]=new Array ("image6","C");
ref[7]=new Array ("image7","C");
ref[8]=new Array ("image8","A");
ref[9]=new Array ("image9","A");
var rlen=ref.length;
2. Each of the sub-arrays is generated on web page load...
<body onload="create_sub_A(); create_sub_B(); create_sub_C();">
3. ...via these functions:
var=sub;
function create_sub_A() {sub="A"; create_new_array(); set_up_gallery();}
function create_sub_B() {sub="B"; create_new_array(); set_up_gallery();}
function create_sub_C() {sub="C"; create_new_array(); set_up_gallery();}
4. which each call:
function create_new_array()
{
sub_Array=new Array();
for (i=0; i<=rlen; i++)
{
x=ref[i][1];
if (sub==x)
{
sub_Array.push(i);
}
slen=sub_Array.length;
}
5. and the gallery set up function, which displays the thumbnail galleries in sub-array-defined groups:
function set_up_gallery()
{
for (i=0; i<=slen; i++)
{
id=i;
document.getElementById(sub+"_thumbnail<img>"+id).src=ref[sub_Array[i]][0];
document.getElementById(sub+"_thumbnail<a>"+id).onclick=larger_image(sub_Array[i]);
console.log(sub+sub_Array[i]);
}
thumbnail gallery A - comprises 4 images (image0,image1,image8,image9)
thumbnail gallery B - comprises 2 images (image2,image4)
thumbnail gallery C - comprises 4 images (image3,image5,image6,image7)
6. Finally, here is the "larger_image" function, which I have yet to get working properly. The problem occurs when I try to define the call to the onclick function as I show in step 5.
function larger_image(a)
{
current_image=a; document.getElementById("lg_image").src=ref[sub_Array[current_image]][0];
}
B. THE CRUX?
a is the value of sub_Array (or is it the "i" within that statement?) for each respective thumbnail - which corresponds to the original ref array index values.
For example, if I wanted to see a larger image0 - I [I]should be able to click on the first image in thumbnail gallery A, which should pass 0 from the thumbnail via onclick="larger_image(sub_Array[i])" - completing src=ref[0][0]; to bring up image0....
However, I can't get onclick defined in such a way as to allow me bring up the larger image (note: thumbnails are currently defined by height="50" in the html).
After I assign the onclick value (see step 5.) and refresh the web page, the large images cycle from first to last, automatically, as if the onclick "button" were being held down as the web page loaded....
console.log(sub+sub_Array[i]);, as defined in step 5. shows that the proper values are being generated by... sub_Array[i] ? ...
A0
A1
B2
A3
B4
C5
C6
C7
A8
A9
...they (the numbers, that is) just aren't being assigned to the onclick event handler properly....
Thanks for any wisdom.