0

I'm having trouble grasping how to use jquery .trigger(). I'm simply looping around checkboxes and finding which checkboxes have been checked. I am then trying to map those values to a key value pair. If I log paymentValue and paymentName I get the correct output which would be something like: cash and true so I want something like cash : true in my key value. Except when I try to run this code I get a paymentName is not defined error

var filterPayment = $('input[name="filter-payment"]:checked');
var paymentObj = {};         

$(document).bind("updateFilters", function() {
    filterPayment.each( function () {
       var paymentValue = paymentObj[this.value];
       paymentValue = this.checked;   

       var paymentName = userObj[this.name];
       paymentName = this.value;             
    });
});

$(document).trigger('updateFilters', {
    paymentValue : paymentName
});

Any ideas?

4
  • 1
    the variable paymentName is not declared... it is declared in the local scope of the each callback Commented Oct 15, 2013 at 2:54
  • Maybe I'm not understanding something how would you declare it outside of the each callback? If I put it above the each callback as just var paymentName I still get the same error. Can you include an example I learn better by seeing some code. Commented Oct 15, 2013 at 2:59
  • what are you trying to do here? are you trying to fire the updateFilters event for each input[name="filter-payment"]:checked element? Commented Oct 15, 2013 at 3:00
  • Your correct I'm trying to fire updateFilters every time there is a checkbox that is checked. Thanks for the help! Commented Oct 15, 2013 at 3:06

1 Answer 1

2

Try

var filterPayment = $('input[name="filter-payment"]');
var paymentObj = {};
var userObj = {};

//listern to the change event
filterPayment.change(function () {
    var paymentValue = paymentObj[this.value];
    paymentValue = this.checked;

    var paymentName = userObj[this.name];
    paymentName = this.value;

    //create a parameter object, since the key is dynamic need to use bracket notation to assign the value
    var param = {};
    param[paymentValue] = paymentName;

    $(document).trigger('updateFilters', param);
}).filter(':checked').change()

$(document).bind("updateFilters", function () {
    alert('filter updated')
});
Sign up to request clarification or add additional context in comments.

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.