Forums

Creating Javascript array in loop (4 posts)

  1. Arsene
    Member
    Posted 2 years ago #

    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 [ http://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 [ http://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 '&nbsp;1&nbsp;'; }
    			?>
    		</span>&nbsp;
    		<span onclick="swap(1, 'imgDefault')" onmouseover="swap(1, 'imgDefault')" class="gal">
    			<?php $thumb = get_post_meta($post->ID, 'Image1', $single = true); if($thumb !== '')
    				{ echo '&nbsp;2&nbsp;'; }
    			?>
    		</span>&nbsp;
        </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.

  2. Arsene
    Member
    Posted 2 years ago #

    And one more thing: I tried to make "galleries" more unique by ithe_ID() tag and it didn't work really well. At least I have unique names for images.

    <!--The image which should be changed-->
    
    <?php $thumb = get_post_meta($post->ID, 'Image0', $single = true); if($thumb !== '')
    	{ echo '<img src="'. $thumb .'" alt="dynamic" width="380" height="380" name="imgDefault'; }
    ?>
    <?php the_ID(); ?>
    <?php $thumb = get_post_meta($post->ID, 'Image0', $single = true); if($thumb !== '')
    	{ echo '" />'; }?>
    ?>
    
    <!--Spans with numbers-->
    
                            <span onclick="swap(0, 'imgDefault<?php the_ID(); ?>')" onmouseover="swap(0, 'imgDefault<?php the_ID(); ?>')" class="gal">
                                <?php $thumb = get_post_meta($post->ID, 'Image0', $single = true); if($thumb !== '')
                                    { echo '&nbsp;1&nbsp;'; }
                                ?>
                            </span>&nbsp;
                            <span onclick="swap(1, 'imgDefault<?php the_ID(); ?>')" onmouseover="swap(1, 'imgDefault<?php the_ID(); ?>')" class="gal">
    							<?php $thumb = get_post_meta($post->ID, 'Image1', $single = true); if($thumb !== '')
                                    { echo '&nbsp;2&nbsp;'; }
                                ?>
  3. Arsene
    Member
    Posted 2 years ago #

    Anyone?

  4. eucopyright
    Member
    Posted 1 year ago #

    Use the raw html plugin, then put your javascript in between the tags...

    [raw]

    <script type="text/javascript">
    You Script;
    </script>

    [/raw]

    WARNING =-> You cannot use the visual editor to open the page ever... or it's screwed and cannot be fixed!

Topic Closed

This topic has been closed to new replies.

About this Topic