0

I have this little script. I need to add and remove elements at my shop basket. So i have a simple counter, but it's not work corectly. When i'm parsing actual number and add another one everythin is fine, but - i can't add another one, and when i'm subctracting it brings me an zero. Can you help me? My code and some fiddle http://jsfiddle.net/5vuJQ/:

$(function(products_counter){
    var n = parseInt($('.lce_number').text());
    var n_place = $('.lce_number');
    $('.lce_add').live('click', function(){
        n_place.empty().append(n + 1);
    });
    $('.lce_remove').live('click', function(){
        n_place.empty().append(n - 1);
    });
});

6 Answers 6

4

You have to pick up the number from .lce_number on each click. Note, live() method was deprecated, you should use on() instead.

var n_place = $('.lce_number');
$('.line_count_elements').on('click', '.lce_add', function() {
    var n = parseInt(n_place.text(), 10);
    n_place.html(n + 1);
});
$('.line_count_elements').on('click', '.lce_remove', function() {
    var n = parseInt(n_place.text(), 10);
    n_place.html(Math.max(0, n - 1));
});​

DEMO: http://jsfiddle.net/5vuJQ/7/

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

9 Comments

If you use delegation, you should unbind too using .off('click') i think
@roasted Why do you think so?
If elements are added dynamically, .on() as delegate will set multi-binds, no?
what if ihave and zero? i don't need -1 element :)
thx man, it works perfect, i'v change it a little bit (jsfiddle.net/5vuJQ/9) but much thx for help, u rox :)
|
1
$(function(products_counter){
    var n = parseInt($('.lce_number').text());
    $('.lce_add').live('click', function(){
       $('.lce_number').text(parseInt($('.lce_number').text())+1);
    });
    $('.lce_remove').live('click', function(){
         $('.lce_number').text(parseInt($('.lce_number').text())-1);
    });
});

Demo here : http://jsfiddle.net/Zzbet/

1 Comment

so, what if ihave and zero? i don't need -1 element :)
1

You could try this instead:

http://jsfiddle.net/5vuJQ/8/

$(function(products_counter){
        var n = parseInt($('.lce_number').text());
        var n_place = $('.lce_number');
        $('.lce_add').on('click', function(){
            n_place.empty().append(++n);
        });
        $('.lce_remove').on('click', function(){
            if(n>0)
              n_place.empty().append(--n);
        });
    });​

Comments

0
$(function(products_counter){
    var n_place = $('.lce_number');
    $('.lce_add').live('click', function(){
        var n = parseInt($('.lce_number').text());
        n_place.empty().append(n + 1);
    });
    $('.lce_remove').live('click', function(){
        var n = parseInt($('.lce_number').text())
        n_place.empty().append(n - 1);
    });
});

Comments

0
$(function(products_counter){      
    var n_place = $('.lce_number');
    $('.lce_add').live('click', function(){
        var n = parseInt($('.lce_number').text());
        n_place.empty().append(n + 1);
    });
    $('.lce_remove').live('click', function(){
        var n = parseInt($('.lce_number').text());
        n_place.empty().append(n - 1);
    });
});

You need to get the value of n after each click!

Comments

0
$('.lce_add').live('click', function(){
        n_place.empty().append(n + 1);
    });

Above lines in your code are not modifying the value of 'n'

To start with - n = 1
On 1st click - append(n + 1) --> 1+ 1 ---> results in 2 being shown
n is still 1
On 2nd click - append(n + 1) --> 1+ 1 --> again results in 2
n is still 1, hence this repeats

You need to modify the value of 'n' before calling append()

 $('.lce_add').live('click', function(){
        n = n + 1;
        n_place.empty().append(n);
    });

http://jsfiddle.net/5tbqX/1/

Comments

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.