I'm building a jQuery function that allows users to add additional fields to a form. The form has four buttons, for different donation values. When you click on one, it generates the appropriate form fields, and adds an option for an additional donation.
For some reason, the additional donation buttons don't work. The code for the buttons is exactly the same, so this makes no sense to me.
Here's the HTML:
<h6>Donation Information</h6>
<p>
<a class="button" data-value="500" href="#" style="margin-right:2px;">5" – $500</a>
<a class="button" data-value="250" href="#" style="margin-right:2px;">4" – $250</a>
<a class="button" data-value="100" href="#" style="margin-right:2px;">3" – $100</a>
<a class="button" data-value="50" href="#" style="padding-right:13px;">2.5" – $50</a>
</p>
<div id="donationTarget"></div>
And here's the jQuery:
$(".button").click(function(e) {
e.preventDefault();
if ($(this).hasClass("clicked")) {
// do nothing
} else if ($(this).hasClass("disabled")) {
// do nothing
} else {
$(this).addClass("clicked");
$(".button:not(.clicked)").addClass("disabled");
var counter = $(".donation").length;
var amount = $(this).attr("data-value");
if (amount == 500) {
var size = 5;
} else if (amount == 250) {
var size = 4;
} else if (amount == 100) {
var size = 3;
} else if (amount == 50) {
var size = 2.5;
}
$("#donationTarget").before("<div class=\"donation\"><input maxlength=\"60\" name=\"inscription" + counter + "\" placeholder=\"Inscription (60 character maximum)\" type=\"text\" /><p><strong>Color Preference (subject to availability):</strong></p><div class=\"half\"><input id=\"color-blue" + counter + "\" type=\"radio\" name=\"color" + counter + "\" /><label for=\"color-blue" + counter + "\">Blue</label><div style=\"clear:both;\"></div><input id=\"color-orange" + counter + "\" type=\"radio\" name=\"color" + counter + "\" /><label for=\"color-orange" + counter + "\">Orange</label><div style=\"clear:both;\"></div><input id=\"color-red" + counter + "\" type=\"radio\" name=\"color" + counter + "\" /><label for=\"color-red" + counter + "\">Red</label><div style=\"clear:both;\"></div></div><!--/.half--><div class=\"half\"><input id=\"color-yellow" + counter + "\" type=\"radio\" name=\"color" + counter + "\" /><label for=\"color-yellow" + counter + "\">Yellow</label><div style=\"clear:both;\"></div><input id=\"color-green" + counter + "\" type=\"radio\" name=\"color" + counter + "\" /><label for=\"color-green" + counter + "\">Green</label><div style=\"clear:both;\"></div></div><!--/.half--><div style=\"clear:both;\"></div></div><br /><h6>Additional Donation</h6><p><a class=\"button\" data-value=\"500\" href=\"#\" style=\"margin-right:2px;\">5\" – $500</a><a class=\"button\" data-value=\"250\" href=\"#\" style=\"margin-right:2px;\">4\" – $250</a><a class=\"button\" data-value=\"100\" href=\"#\" style=\"margin-right:2px;\">3\" – $100</a><a class=\"button\" data-value=\"50\" href=\"#\" style=\"padding-right:13px;\">2.5\" – $50</a></p>");
}
});
Also, is there a way to clean up the super long line of jQuery towards the end of the function? I can't figure out how to keep the HTML on multiple lines without breaking the script.
Thanks.
jsFiddle: http://jsfiddle.net/aptwt/2/