Jay
Jay

Reputation: 3393

give numbering to each div with same class name

I have around 10 div elements with same class name e.g.

<div class="div1"></div>
<div class="div1"></div>
<div class="div1"></div>
<div class="div1"></div>
<div class="div1"></div>
<div class="div1"></div> <!-- and so on.... -->

I want to give numbering to each div e.g.

<div class="div1">1</div>
<div class="div1">2</div>
<div class="div1">3</div>
<div class="div1">4</div>
<div class="div1">5</div>

Help appreciated.

Upvotes: 0

Views: 119

Answers (2)

GautamD31
GautamD31

Reputation: 28763

Try like this

$('.div1').each(function(){
     $(this).text($(this).index() + 1);
});

See this FIDDLE

Upvotes: 3

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382150

You can do this :

$('.div1').html(function(i){ return i+1 });

See documentation of the html function and demonstration.

Upvotes: 7

Related Questions