javascript - How to loop through all elements jQuery -
i have images , have count facebook shares each image. can't make loop that. it's 1 element. how that? code is:
<script> $(document).ready(function(){ var current = 0; $count = $('#count_bombs').val(); $('#' + current).each(function() { var id = $('#' + current).val(); console.log(id); $.getjson('https://graph.facebook.com/http://example.com/images/view/' + id, function(data) { document.getelementbyid("shares_count_" +current).innerhtml = data.shares; console.log(data.shares); console.log("#shares_count_" +current); }); current++; }); }); </script>
<?php $num=0; $id = 0; ?> <?php foreach($images $key=>$val){ ?> <div> <a href="https://www.facebook.com/sharer/sharer.php?u=<?php echo url::base();?>images/view/<?php echo $val['image_name'];?>"> <span aria-hidden="true" class="glyphicon glyphicon-link"></span> <input type="hidden" id="<?php echo $id; ?>" class="count" value="<?php echo $val['image_name'];?>"> share </a> </div> <div> <span class="glyphicon glyphicon-comment" aria-hidden="true" ></span> <span id="shares_count_<?php echo $id; ?>"></span> <input type="hidden" value="<?php echo count($bombs);?>" id="count_bombs" name="count_bombs"> </div>
edited: did it. used index
parameter of each method. code:
<script> $(document).ready(function(){ var current = 0; $("input[type=hidden]").each(function(index, element){ var id = $('#' + current).val(); $.getjson('https://graph.facebook.com/http://pbombd.dev/bombs/view/' + id, function(data) { $('#shares_count_' + index).html(data.shares); }); current++; }); }); </script>
$('#' + count)
returns 1 element can't loop on it. seems want find every hidden input css class count
in page. can :
$('input.count').each(function() { $input = $(this); // $input contains hidden input console.log($input.attr('id)); });
Comments
Post a Comment