javascript - Fill data into all of my spans -
i have 8 spans, 4 of spans values retrieved database visible when page loads. other spans in own popup box, when user clicks on button view popup, value spans visible needs inside spans within popup box.
so used each() function 2 classes, '.heart' class has first 4 spans visible when page loads. '.likes-count' class has 4 spans within popup box.
my aim assign whatever value in first 4 spans other spans within popup box.
im stuck in code below.
js
$('.heart, .likes-count').each(function(i, element) { var thisid = "#" + $(this).attr('id'); // '#like1', '#like2' var getagain = "." + $(thisid + " span").attr('class'); // .likecount1 getagain = $(getagain).text(); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
you can iterate through spans of first div using jquery's each. callback function of each
gives index parameter can use select corresponding indexed span of other div.
$("#hearts span").each(function(index){ var text = $(this).text(); $("#likes span").eq(index).text( text ); });//each
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="hearts"> <span>12</span> <span>24</span> <span>36</span> <span>48</span> </div> <div id="likes"> <span></span> <span></span> <span></span> <span></span> </div>
Comments
Post a Comment