0

I'm trying to display count within the 'dt' wrap of each person but at the moment its displaying count between everyone in and outside the 'dt' wrap. I've attached a screen print. if anyone can advice me on this fix I'd greatly appreciate it.

 $('.order_table_item .variation dt').each(function () {
        if ($(this).text() == 'Candidate Name:') {
            $(this).next().show();
            $(this).before("<div class='count'></div>");
            $(this).next().after("<div class='clear'></div>");

        } else {
            $(this).hide();
        }
    });

    $('.variation .count').each(function(i) {
            $(this).append((i+1));
    });

enter image description here

I tried,

$('.order_table_item').each(function(i,e){


    $('.variation .count').each(function(i) {
            $(this).append((i+1));
    });

});

and now its doubling up the numbers,

enter image description here

The cleaned up shorted down version of my html,

  <table class="shop_table order_details">
    <tbody>
        <tr class="order_table_item">
            <td class="product-name"> 
                <dl class="variation">
                    <div class="count" style="border-top: 0px none;">11</div>
                    <dd style="display: block; border-top: 0px none;">Dave</dd>
                    <div class="clear"></div>
                </dl>
            </td>
        </tr>
        <tr class="order_table_item">
            <td class="product-name">
                <dl class="variation">
                    <div class="count">22</div>
                    <dd style="display: block;">JACK</dd>
                    <div class="clear"></div>
                    <div class="count">33</div>
                    <dd style="display: block;">JOHN</dd>
                </dl>
            </td>
        </tr>
    </tbody>
</table>
3
  • 3
    show us your html code as well Commented Sep 10, 2013 at 11:37
  • $('.variation .count') change it to $('.variation dt .count'); Commented Sep 10, 2013 at 11:48
  • On your fiddle, should the output go: 111, 221, 332? Commented Sep 10, 2013 at 14:25

2 Answers 2

1

If I've understood correctly:

DEMO: http://jsfiddle.net/7DxVJ/1/

JQuery

$('.variation').each(function(x) {
    $(this).find('.count').each(function(i) {
        $(this).append(i+1);
    });
});

Outputs:

111 Dave

221 JACK

332 JOHN

Sign up to request clarification or add additional context in comments.

Comments

0

Since i dont know exactly how your code looks like, i have three ideas:

Idea 1:

$('.variation .count').each(function(i) {
  $(this).text(i+1);
});

Idea 2: Since "1" + 1 = 11, try to parse the i so it becomes an int instead of a string.

$('.variation .count').each(function(i) {
  $(this).append(parseint(i)+1);
});

Idea 3:

$('.variation .count').each(function(index) { //i is already taken, better to use another variable here.
  $(this).text(index+1);
});

Idea 4: Since you are inside an each-loop, which i missed, you probably want to loop through each of its .count, not all .count everytime.

$('.order_table_item').each(function(i,e){
  $(this).find('.variation .count').each(function(index) { //use $(this) to loop every .count inside .order_table_item.
    $(this).text(index+1);
  });
});

See working fiddle

1 Comment

I tried all of these and they all output the same issue, the either repeat the number e.g 11 ,22 ,33 or it goes 1,2,3 from div to div instead of going box one 1, box two 1,2,3. I've added my html above if they is anything else you can see please let me know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.