Federico Ferrari Federico Ferrari - 1 month ago 6
Javascript Question

Return single number of divs not just their total

If I have this function that gives me that TOTAL number of divs, how can I change it to have a result like this: 01 02 03 04 05 06 07?

Thanks in advance



$(document.body).ready(function() {
$(document.body).append($(".slide"));
var n = $(".slide").length;
$("span").text("0" + n);
})

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

<span></span>

<div class=slide></div>
<div class=slide></div>
<div class=slide></div>
<div class=slide></div>
<div class=slide></div>
<div class=slide></div>
<div class=slide></div>




Answer

Updated fiddle

$(document.body).ready(function() {
    $( document.body ).append( $( "<div>" ) );
    var n = $( "div" ).length;
    
    for(var i=0; i<n; i++){
    $("span" ).append( "0" + i+'\u00A0');
    }
  })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span></span>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>

Comments