/    Sign up×
Community /Pin to ProfileBookmark

count nr of div’s with class

Id like to count all divs with class ‘count’ then id like to span each divs nr

[code=php]<div class=”count”>
<span class=”count_nr”> XX</span>
</div>
<div class=”count”>
<span class=”count_nr”> XX</span>
</div>
<div class=”count”>
<span class=”count_nr”> XX</span>
</div>
<div class=”count”>
<span class=”count_nr”> XX</span>
</div>[/code]

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@cbVisionJan 04.2013 — Here's a simple jQuery solution:

http://jsfiddle.net/B9qpm/1/

Not sure what you mean by "id like to span each divs nr."
Copy linkTweet thisAlerts:
@NogDogJan 05.2013 — Within PHP, you could use the [url=http://php.net/dom]DOM extension[/url] to do this, I would think.
Copy linkTweet thisAlerts:
@chisburgauthorJan 05.2013 — Here's a simple jQuery solution:

http://jsfiddle.net/B9qpm/1/

Not sure what you mean by "id like to span each divs nr."[/QUOTE]


Ahh if i can solve it with jQuery thats great! What I ment by "id like to span each divs nr." is that 'XX' should represent the actual number of the div. So the first div would have '01' instead of XX, the second '02'…
Copy linkTweet thisAlerts:
@DerokorianJan 05.2013 — Tested jquery on jsfiddle -> http://jsfiddle.net/ZuDJD/ :

<i>
</i>&lt;script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript"&gt;
function zeroPad(num, places) {
var zero = places - num.toString().length + 1;
return Array(+(zero &gt; 0 &amp;&amp; zero)).join("0") + num;
}
$(function() {
$('.count').each(function (i, div) {
$(div).children('.count_nr').html(zeroPad(i+1, 2));
});
}); <br/>
&lt;/script&gt;
×

Success!

Help @chisburg spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...