I'm currently looping around a series of checkboxes that have a specific name assigned to them. Whatever checkbox is checked it gets the value and puts it in an array.
var filterHeight = $('input[name="filter-height"]:checked');
var userArray = [];
filterHeight.each(function(){
userArray.push($(this).val());
});
So the output could be something like short, medium, tall
How would I assign a key value to the array values? I'm looking to do something like:
$(document).trigger('showHeight', {
short: true,
medium: false,
tall: false
}
});
Any help is appreciated!
asked Oct 14, 2013 at 23:34
Delmon Young
2,0737 gold badges40 silver badges51 bronze badges
1 Answer 1
You need an object so try:
var filterHeight = $('input[name="filter-height"]');
var userObj = {};
filterHeight.each(function () {
userObj[this.value] = this.checked;
});
$(document).trigger('showHeight', userObj);
answered Oct 14, 2013 at 23:38
PSL
124k21 gold badges257 silver badges243 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-js
userArray[$(this).val()] = true;