Creating Javascript array in loop
-
Hi,
after hours of googling I decided to post a new thread. I use onmouseover “galleries” in my loop – and they ar (of course) working just great if there is just one post [ https://petrcibulka.freshflesh.cz/?cat=2, just onmouseover on numbers bellow “current web project” header ]. Images are provided by custom fields.
The problem is that sript can’t recognize that he needs to generate new “gallery” because there is a new post. Sorry for not using proper words, but I think this is the better way to understand the problem. And here goes the code [ https://petrcibulka.freshflesh.cz/?cat=3 ]:
<!--script for "gallery"--> <script type="text/javascript"> var aryImages = new Array(1); var selIndex = 0; aryImages[0] = "<?php $thumb = get_post_meta($post->ID, 'Image0', $single = true); if($thumb !== '') { echo ''. $thumb .''; } ?>" aryImages[1] = "<?php $thumb = get_post_meta($post->ID, 'Image1', $single = true); if($thumb !== '') { echo ''. $thumb .''; } ?>" for (i=0; i > aryImages.length; i++) { var preload = new Image(); preload.src = aryImages[i]; } function swap(imgIndex, imgTarget) { document[imgTarget].src = aryImages[imgIndex]; selIndex = imgIndex; } </script> <!-- Code inside the loop, let's say it concerns three posts--> <table><tr> <table><tr> <td> <a href="<?php the_permalink(); ?>"><?php $thumb = get_post_meta($post->ID, 'Image0', $single = true); if($thumb !== '') { echo '<img src="'. $thumb .'" alt="dynamic" name="imgDefault" width="380" height="380" border="0" />'; } ?></a> </td><td> <span onclick="swap(0, 'imgDefault')" onmouseover="swap(0, 'imgDefault')" class="gal"> <?php $thumb = get_post_meta($post->ID, 'Image0', $single = true); if($thumb !== '') { echo ' 1 '; } ?> </span> <span onclick="swap(1, 'imgDefault')" onmouseover="swap(1, 'imgDefault')" class="gal"> <?php $thumb = get_post_meta($post->ID, 'Image1', $single = true); if($thumb !== '') { echo ' 2 '; } ?> </span> </td> </tr></table>
As you can see, “gallery” works just for last post. Any suggestions? I don’t stick to this particular sollution of Javascript array but I don’t want to use any plugin for this and get it work just with custom fields. As far as I know, common onmouseover atribute doesn’t work “distantly” – and I need to change the image which is outside the tag (no matter if anchor or span tag).
Any help would be very superappreciated.
- The topic ‘Creating Javascript array in loop’ is closed to new replies.