Skip to content

Instantly share code, notes, and snippets.

@salsalabs
Last active May 15, 2017 16:58
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 salsalabs/93b9b6241d34efea1d3b to your computer and use it in GitHub Desktop.
Save salsalabs/93b9b6241d34efea1d3b to your computer and use it in GitHub Desktop.
"Click" the applicable donation amount on a Salsa donation page when passing an amount via the URL. Also saves the amount so that it appears when errors happen on a donation page.
<script type="text/javascript">
(function () {
// SalsaStaff 81331: If "amount=number" is in the URL, then click the radio button
// for that amount. If a radio button does not exist, then put 'number' into the
// "other" field.
//
// This script captures the amount at submit and stores it in the browser's local
// storage. If there are errors passed back from Salsa, then the amount is retrieved
// from the browser's local storage. Local storage is cleaned after retrieving the value.
//
// If there's not an 'amount' parameter in the URL, *and* there not an amount in local
// storage, then then a default radio button is checked (nominally $50).
//
// Function to search the donation page for an amount.
// @return Number|null Value for the amount
function getAmount() {
var amount = parseFloat(document.querySelector("#otheramt").value);
if (isNaN(amount)) {
amount = Array.from(document.querySelectorAll('input[name="amount"]'))
.filter(function(e) { return e.checked; })
.reduce(function(a, e) { a = e.value; return a; }, 0);
};
return amount;
}
// Function to click the radio button for amount the provided amount. If
// no radio button exists, then put the amount into the "other amount" field.
// @param Number a amount to store
function setAmount(amount) {
if (!isNaN(amount)) {
var otherAmount = document.querySelector("#otheramt");
var field = document.querySelector("#amt" + amount);
if (field != null) {
field.click();
otherAmount.value = '';
} else {
document.querySelector('#other').click();
otherAmount.click();
otherAmount.value = amount;
}
}
}
$(document).ready(function () {
var defaultAmount = 50;
if (document.querySelector('input[name="donate_page_KEY"]') != null) {
var m = RegExp('amount=(\\d+)').exec(window.location.search);
var amount = (m != null) ? m[1] : defaultAmount;
// If there are errors, then it's time to retrieve the amount
// from localStorage.
if ($('.error').length > 0) {
var x = window.localStorage.getItem('abc');
console.log('Retrieved ' + x);
if (x != null) {
window.localStorage.removeItem('abc');
var a = parseFloat(x);
amount = !isNaN(a) ? a : amount;
}
}
setAmount(amount);
$('.salsa form').submit(function(e) {
// e.preventDefault();
amount = getAmount();
if (!isNaN(amount)) {
window.localStorage.setItem('abc', '' + amount);
console.log('Stashing ' + amount);
}
return true;
})
}
});
})();
</script>
@ejusa
Copy link

ejusa commented Apr 17, 2017

One problem that's just come up with this is that when a supporter gets an error (because they mis-typed credit card or something), their donation amount resets to this default, rather than what they already selected before their initial attempt to submit. Is there a quick fix?

@salsalabs
Copy link
Author

Hi, Sarah! Fixed that in the most recent release. You'll need to replace the whole script in order to take advantage of the change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment