Skip to content

Instantly share code, notes, and snippets.

@viralgh
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save viralgh/fe31c078032aec34efea to your computer and use it in GitHub Desktop.
Save viralgh/fe31c078032aec34efea to your computer and use it in GitHub Desktop.
$(function() { // shorthand for $(document).ready()
// get all checkboxes
var allCheckboxes = $(".perk:checkbox");
// group them by name for later
var groups = {};
allCheckboxes.each(function() {
groups[this.name] || (groups[this.name] = []);
groups[this.name].push(this);
});
// hook up event handling
allCheckboxes.on("change", function(event) {
// reset all checkboxes (easier than having to reset)
allCheckboxes.each(function() {
this.checked = false
});
// get the clicked checkbox and its amount
var selected = $(this).prop("checked", true);
var amount = selected.data("amount");
// process each group of checkboxes
for (var name in groups) {
if (!groups.hasOwnProperty(name)) {
continue
}
var group = $(groups[name]);
// get those checkboxes in the group that have an equal or lower amount
var matching = group.filter(function() {
return $(this).data("amount") <= amount;
});
// get the maximum within that
var amounts = matching.map(function() {
return $(this).data("amount");
}).get();
var maxAmount = Math.max.apply(null, amounts);
// now check those that have that amount
group.filter("[data-amount='" + maxAmount + "']").prop("checked", true);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment