Help using javascript in archive loop
-
Hi folks,
I’m a complete beginner with Javascript. I know there is a way of doing this.I have a custom archive page. I echo the thumbnail URL in an invisible div on each item. I put that URL into a javascript variable with a function. When user clicks a button, the function executes and it injects the URL into the src of an
tag outside of the loop.
This works great, except I need to iterate the name of my JS functions as well as the id names so the first posts thumbnail isn’t used by every function. I tried doing this by appending <?php echo get_the_ID();?> to my function name, which results in myFunction123, but the console reports “myFunction is undefined”.
Any ideas?
<button onclick="tester()">Tester</button> <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?> <div id="invis-image<?php echo get_the_ID();?>"><?php echo $image[0];?> </div> <script> function tester<?php echo get_the_ID();?>() { var invisimg = document.getElementById("invis-image<?php echo get_the_ID();?>").innerHTML; document.getElementById("player-img").src = invisimg; } </script>
- The topic ‘Help using javascript in archive loop’ is closed to new replies.