JavaScript Array syntax question

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Nov 2007
Posts: 4
Reputation: rebsysue is an unknown quantity at this point 
Solved Threads: 0
rebsysue's Avatar
rebsysue rebsysue is offline Offline
Newbie Poster

JavaScript Array syntax question

 
0
  #1
Mar 3rd, 2009
Hi. I am just learning javascript and am making a website for my friend's photography. I've got a working slide show now where you can click next or previous to display image after image in sequence. I followed this tutorial:
http://www.webmonkey.com/tutorial/Ma...ript_Slideshow

Now I would like to also display a caption or description with each photograph. Right now I'm using an array like this:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. var imageNum = 0;
  2. imageArray = new Array();
  3. imageArray[imageNum++] = new imageItem(imageDir + "roma072.jpg");
  4. imageArray[imageNum++] = new imageItem(imageDir + "roma073.jpg");
  5. imageArray[imageNum++] = new imageItem(imageDir + "timo1.jpg");
  6. var totalImages = imageArray.length;

I am having trouble looking on the web for the right syntax, if there is any, for adding title information that I can output later for each image in the array. For example, within this array, how can i add that the first image, "roma072.jpg" has a title or description of "Rome, 2008"?

Any help is much appreciated.

Thank you,
Rebecca
Last edited by peter_budo; Mar 4th, 2009 at 7:53 pm. Reason: Code tag correction, please use [code][/code] instead of <code></code>
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 954
Reputation: essential will become famous soon enough essential will become famous soon enough 
Solved Threads: 131
Featured Poster
essential's Avatar
essential essential is offline Offline
Posting Shark

Re: JavaScript Array syntax question

 
0
  #2
Mar 4th, 2009
Try this instead! If im goin to make some slideShow (or image swapping) this is what i usually do. Its up to you if you wanna try my demo. I've provided different titles for every image that will get to be swapped.
  1. <html>
  2. <head>
  3. <title>Arrays and Putting title on your swapped image</title>
  4. <script type="text/javascript">
  5. <!--
  6. var i = 0;
  7.  
  8. function putTitle() {
  9. var imgArray = [];
  10. var img = document.getElementById('swap');
  11. var myTitle = document.getElementById('imgTitle');
  12. if (document.images) {
  13. /* Total n# of images to be swapped or icremented in which 4 is my incrementing value on this demo. */
  14.  
  15. for (var x = 0; x < 4; x++) {
  16. imgArray[x] = new Image();
  17. /* You must provide a valid path and a sequential n# with your images filename (e.g. image1, image2, image3, and so on...). */
  18. imgArray[x].src = 'image' + x + '.jpg'; }
  19. if (imgArray.length > 1) {
  20. img.src = imgArray[i].src;
  21. i++;
  22. }
  23. } switch(i) {
  24. case 1 : img.title = 'Title1'; break;
  25. case 2 : img.title = 'Title2'; break;
  26. case 3 : img.title = 'Title3'; break;
  27. case 4 : img.title = 'Title4'; break;
  28. default : img.title = 'Original Title'; break; }
  29. myTitle.innerHTML = img.title;
  30. i = ( i == 4 ) ? 0 : i;
  31. }
  32.  
  33. //-->
  34. </script>
  35. </head>
  36. <body>
  37. <img src="image.jpg" id="swap">
  38. <br>
  39. <button onclick="putTitle();">Click</button>
  40.  
  41. <!-- Let's see the title output of every image on the swapping sequence, by showing their title on the div below -->
  42. <div id="imgTitle">Current Title Of Your Image</div>
  43. </body>
  44. </html>
  45.  
Hope you find this usefull...
Last edited by essential; Mar 4th, 2009 at 1:08 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 7
Reputation: cubekid is an unknown quantity at this point 
Solved Threads: 0
cubekid cubekid is offline Offline
Newbie Poster

Re: JavaScript Array syntax question

 
0
  #3
Mar 4th, 2009
I turned the imageArray array to a 2-dimensional array

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. var imageNum = 0;
  2. imageArray = new Array();
  3.  
  4. imageArray[imageNum] = new Array();
  5.  
  6. imageArray[imageNum][0] = new imageItem(imageDir + "roma072.jpg");
  7. imageArray[imageNum][1] = "Caption1";
  8.  
  9. imageNum++;
  10. imageArray[imageNum] = new Array();
  11.  
  12. imageArray[imageNum][0] = new imageItem(imageDir + "roma073.jpg");
  13. imageArray[imageNum][1] = "Caption2";
  14.  
  15. imageNum++;
  16. imageArray[imageNum] = new Array();
  17.  
  18. imageArray[imageNum][0] = new imageItem(imageDir + "timo1.jpg");
  19. imageArray[imageNum][1] = "Caption3";
  20.  
  21. var totalImages = imageArray.length;

then, in the getPrevImage() and getNextImage() function, please revise this line:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. var new_image = get_ImageItemLocation(imageArray[imageNum]);

with this
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. var new_image = get_ImageItemLocation(imageArray[imageNum][0]);
  2. document.all("caption").innerText = imageArray[imageNum][1];

To display the caption, i added a span tag
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <span id = "caption" name="caption"></span>

That's it. Hope this helps you.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



Tag cloud for JavaScript / DHTML / AJAX
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC