How to write fraction inside each span that represents index of the parent div / total number of divs?

Using jquery, how do I write a fraction inside the span that represents index of the parent div / total number of divs with class item?

<div class="item">
    <span>1/3</span>
</div>
<div class="item">
    <span>2/3</span>
</div>
<div class="item">
    <span>3/3</span>
</div>

Answers 1

  • Try this :)

    var div_tot = $('div.item').length; //count total of divs
    
    $('div.item').each(function(i) {
        var no = i+1;
        $(this).find('span').text(no+'/'+div_tot);
    });
    

Related Articles