Last active
August 29, 2015 14:15
-
-
Save viralvadgama/fe31c078032aec34efea to your computer and use it in GitHub Desktop.
Working demo: http://jsfiddle.net/j051rkkh/1/ new: http://jsfiddle.net/j051rkkh/3/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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