www.webdeveloper.com
Results 1 to 5 of 5

Thread: count nr of div's with class

  1. #1
    Join Date
    Feb 2011
    Posts
    47

    count nr of div's with class

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

    PHP Code:
    <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

  2. #2
    Join Date
    Sep 2008
    Location
    Akron, OH
    Posts
    1,111
    Here's a simple jQuery solution:
    http://jsfiddle.net/B9qpm/1/

    Not sure what you mean by "id like to span each divs nr."
    I'm always up for networking with fellow web professionals. Connect with me on LinkedIn if you like!

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,148
    Within PHP, you could use the DOM extension to do this, I would think.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Feb 2011
    Posts
    47
    Quote Originally Posted by cbVision View Post
    Here's a simple jQuery solution:
    http://jsfiddle.net/B9qpm/1/

    Not sure what you mean by "id like to span each divs nr."
    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'

  5. #5
    Join Date
    Oct 2010
    Location
    Ohio
    Posts
    233
    Tested jquery on jsfiddle -> http://jsfiddle.net/ZuDJD/ :

    Code:
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script type="text/javascript">
    function zeroPad(num, places) {
      var zero = places - num.toString().length + 1;
      return Array(+(zero > 0 && zero)).join("0") + num;
    }
    $(function() {
      $('.count').each(function (i, div) {
         $(div).children('.count_nr').html(zeroPad(i+1, 2));
      });
    });  
    </script>
    ~Ryan
    www.rdennispallas.com <-- Personal Site, changing regularly cuz its ugly.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles