Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

<div>first div</div>
<div>second div</div>
<div>third div</div>

every div need to show his order and be like this

<div>first div <span>1</span></div>
<div>second div <span>2</span></div>
<div>third div <span>3</span></div>

This html example code in jsfiddle

http://jsfiddle.net/CtDLe/

I need the output to be like this using jQuery

http://jsfiddle.net/CtDLe/1/

like image 513
Jim Avatar asked Aug 14 '13 15:08

Jim


1 Answers

Simple each loop does the trick:

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

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

like image 83
tymeJV Avatar answered Oct 06 '22 23:10

tymeJV