RDK
RDK

Reputation: 4560

Use jquery function .on

How to use .on on load event more then one time?

var $CartContent = function(){
    $Handler = $('#cartHandler');
    if($Handler.length > 0){
        $Handler.load('cart/content', function(){
            $PercentField = $('#item-percent-field');
            $PercentField.val(0);
            $('.percent-field').val(0);
            $PercentField.on('keyup', function(){
                var $Value = parseInt($(this).val());
                $.each($('.percent-field'), function(index, value){
                    var $Current = $(this).data('item');
                    $.post('cart/content', {
                        id: $Current,
                        percent: $Value
                    });
                });
            });
        });
    }
};

It work's fine only one time. How to start function load with parameters on every keyup?

Upvotes: 0

Views: 96

Answers (2)

Denys Séguret
Denys Séguret

Reputation: 382112

You may use the keyup function :

$('#item-percent-field').keyup(function(){
    $('#cartHandler').load( ...
    ...
});

But be sure to have a fast server if you want to load a page on every key up.

Regarding the whole problem, it's not easy to tell what you're trying to do.

First a few remarks :

  • do you really want to have a function named $CartContent ? Why ?
  • you don't need to test if($Handler.length > 0){ : if empty the following line would simply do nothing
  • do you want to make a post request for every percent-field each time there is one changed ? This is strange

Upvotes: 2

Samuel Cook
Samuel Cook

Reputation: 16828

I would just move the $PercentField function out altogether and switch to the .live() method, since #item-percent-field won't be created until after the .load is complete

$(function(){
    $('#item-percent-field').live('keyup',function(){
        var $Value = parseInt($(this).val());
        $.each($('.percent-field'), function(index, value){
            var $Current = $(this).data('item');
            $.post('cart/content', {
                id: $Current,
                percent: $Value
            });
        });
    })
    var $CartContent = function(){
        $Handler = $('#cartHandler');
        if($Handler.length > 0){
            $Handler.load('cart/content', function(){
                $PercentField = $('#item-percent-field');
                $PercentField.val(0);
                $('.percent-field').val(0);;
            });
        }
    });
});

Upvotes: 0

Related Questions