0

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?

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

5 Comments

Nothing really wrong but when you have a selector with id, and which isn't dynamically added, there is no reason to take the long (and slow) path.
Аfter $PercentField.on('keyup', function(){}); I need to update results with .load $Handler.load('cart/content') after this my code no working
What you want to do is unclear for me. What's the logic of those requests ?
What's the logic of those requests ? Update Session Data, and get data
But why do you make, each time one field is changed, one post request per field ? Is this a feature or a bug ?
0

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);;
            });
        }
    });
});

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.