Created
July 12, 2016 21:13
-
-
Save pfeilbr/810492ed9245a44e622cc9bff8d7de6b to your computer and use it in GitHub Desktop.
add to cart prerequisite validation
This file contains hidden or 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
jQuery(function($) { | |
function validatePrerequisites() { | |
// <select /> DOM ID to item label map | |
var selectDOMIdToLabelMap = { | |
"free-t-shirt-size": "Free T Shirt Size", | |
"power-protein-flavor": "Power Protein Flavor", | |
"pump-powder": "Pump Powder" | |
}; | |
// check if each item has a selected value | |
var itemsNotSelected = []; | |
for (var selectDOMId in selectDOMIdToLabelMap) { | |
var $select = $("select#" + selectDOMId); | |
var $tr = $select.parent().parent(); | |
// clear warning if exists | |
$tr.find("td.warn").remove(); | |
if ($select.val() === "") { // empty, so add it to the list of items without a selection | |
itemsNotSelected.push(selectDOMIdToLabelMap[selectDOMId]); | |
$tr.append('<td class="warn" style="color: red; font-size: small;">Please choose an option</td>'); | |
} | |
} | |
// return true if all items have a selected value; otherwise false | |
return itemsNotSelected.length === 0; | |
} | |
// button handler | |
$("button.placeholder_button").click(function(e) { | |
var valid = validatePrerequisites(); | |
// don't submit for if not valid | |
if (!valid) { | |
e.preventDefault(); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment