How can I echo each item in my PHP array rather than just the last? -
i have array in php (it has php complicated reasons go beyond scope of post):
<?php $arr = array(div1, div2); foreach ($arr $value) { print_r($value); } ?>
i have jquery i'm attempting use hide every element has class in array.
<script> $(document).ready(function(){ $("."+"<?php echo $value; ?>").hide(); }); </script>
however, hides elements class equivalent last item in array. in other words, items class div2
hide. how can make apply each item in array?
you can take php array , use in javascript. easiest way use json:
<?php $arr = array('div1', 'div2'); ?> <script> $(document).ready(function(){ var arr = <?=json_encode($arr);?>; arr.foreach((classname)=>$("."+classname).hide()); }); </script>
or can join array , let jquery iterate on it:
<script> $(document).ready(function(){ var arr = <?=json_encode($arr);?>; $('.'+arr.join(', .')).hide(); }); </script>
Comments
Post a Comment