Skip to content Skip to sidebar Skip to footer

Count All Div Elements And Add Each Number Inside Span Using Jquery

I need to show each number of div in the page in order And add the value of each div inside span so if I have 4 divs inside page like this
first div

Solution 1:

Simple each loop does the trick:

$("div").each(function(i) {
    $(this).find("span").text(++i);
});

Demo: http://jsfiddle.net/CtDLe/3/

Solution 2:

$("div").each(function(idx,elem) {
    $("<span>").text(idx).appendTo(wherever);
});

Solution 3:

You can try this:

$("div").each(function(i, elem){
 $(elem).append($("<span>"+(i+1)+"</span>"));
});

JSFiddle example

Solution 4:

var counter = 1;
$('h1').each(function () {
    $(this).find('span').html(counter);
    counter++;
});

jsFiddle example

Post a Comment for "Count All Div Elements And Add Each Number Inside Span Using Jquery"