javascript - Show / Hide Function within while loop -
i have while loop spits out data advanced custom fields.
within while loop have read more / read less button displays additional field if clicked.
the problem have there no unique id div, when clicking read more button of sections open, not 1 selected.
the html looks -
<?php if(have_rows('features')) : ?> <div class="features-container"> <?php while(have_rows('features')) : the_row(); ?> <div class="feature"> <?php if(get_row_index() % 2 == 0) : ?> <div class="feature-image" style="background-image: url('<?php the_sub_field('image'); ?>');"> <div class="categories-tag-left"><?php the_sub_field('category'); ?></div> </div> <?php endif; ?> <div class="feature-description"> <div class="featured-description-container"> <h3><?php the_sub_field('name'); ?></h3> <?php the_sub_field('introduction'); ?></br> <div class="slidingdiv"> <?php the_sub_field('more'); ?> </div> <div class="read-more-button-container"> <a href="#" class="show_hide tech-read-more-button">read more</a> </div> </div> </div> <?php if(get_row_index() % 2 != 0) : ?> <div class="feature-image" style="background-image: url('<?php the_sub_field('image'); ?>');"> <div class="categories-tag-right"><?php the_sub_field('category'); ?></div> </div> <?php endif; ?> </div> <?php endwhile; ?> </div> <?php endif; ?>
the javascript have looks -
$(".slidingdiv").hide(); $('.show_hide').click(function (e) { $(".slidingdiv").slidetoggle("slow"); var val = $(this).text() == "read more" ? "read less" : "read more"; $(this).hide().text(val).fadein("fast"); e.preventdefault(); });
can solution?
you can use this
current node , jquery functions prev
, parent
selecting desired element
<script type="text/javascript"> $(".slidingdiv").hide(); $('.show_hide').click(function (e) { $(this).parent().prev().slidetoggle("slow"); var val = $(this).text() == "read more" ? "read less" : "read more"; $(this).hide().text(val).fadein("fast"); e.preventdefault(); }); </script>
Comments
Post a Comment