0

How can i count element once? Right now, evert time, when i click #totalItems, alert is rised as many times, as many element are in #photoId ?

<script type="text/javascript">
    $(document).ready(function(){ 
        photoId = $('.photoId');
        totalItems = $('#totalItems');

        $(photoId).on('click', function(){
              //alert ($(this).html()); 
              $(this).clone().appendTo(totalItems);

              $('#count').on('click', function(e){
              sizes = (totalItems.children().size()); 
              alert (sizes);
              });

              $('#count').off('click', function(e){
              sizes = (totalItems.children().size()); 
              alert (sizes);
             });       
        });          
    });
</script>
Naftali
  • 144,921
  • 39
  • 244
  • 303
user2610146
  • 179
  • 1
  • 11

2 Answers2

0

Replace this line :

$(photoId).on('click', function(){

By :

photoId.on('click', function(){
Oliboy50
  • 2,661
  • 3
  • 27
  • 36
  • No, i have created loop. This : $('#count').on('click', function(e){ should be on its own code, not in $(photoId).on('click', function(){. That was soure of bug. Thank you all for help. – user2610146 Sep 13 '13 at 14:00
0

You are attaching $('#count').on('click', function() { ... handler on every click on .photoId. Make sure it is only done once from within the document's ready event.

In case your #count is actually inside the DOM being cloned, you need to

  1. get rid of ID and replace it with class.
  2. bind event to the top level container with CSS filter

I.e. something like this:

$("#totalItems").on("click", ".count", function() { ...
Ilia G
  • 10,043
  • 2
  • 40
  • 59