After taking advice from Stephen Muecke I was able to build a solution to my problem.
On my page, I have dynamically added checkboxes that when their checked property changes a function is called to either add or remove text to/from a <label>
.
This text was then to be added to, or removed from, the href
of a button
.
The solution I came up with was to write a couple of functions that handle all the logic I require.
Checkbox On Change event:
$('input[type="checkbox"].style2').checkbox().on('change', function () {
var compareRiskLinkObject = $('#lnkCompareRisks');
var compareRiskLinkHref = compareRiskLinkObject.attr('href');
var QuoteRef = "";
var checkboxID = $(this).attr('id');
QuoteRef = $(this).siblings('#hdfQuoteRef').val();
if ($(this).prop('checked')) {
checkAndStoreToAvailableRiskSlot(QuoteRef, checkboxID, compareRiskLinkObject, compareRiskLinkHref);
} else {
checkAndRemoveFromRiskSlot(QuoteRef, compareRiskLinkObject, compareRiskLinkHref);
}
});
Functions called:
function checkAndStoreToAvailableRiskSlot(quoteRef, checkboxID, linkbutton, href) {
// get IDs of 3 risk slots
var risk1 = $('#lblQuote1');
var risk2 = $('#lblQuote2');
var risk3 = $('#lblQuote3');
var hdf1 = $('#hdfQuote1');
var hdf2 = $('#hdfQuote2');
var hdf3 = $('#hdfQuote3');
if (risk1.text() == "") {
risk1.text(quoteRef);
hdf1.val(checkboxID);
appendHrefWithRisks(linkbutton, href, "risk1=" + quoteRef);
} else if (risk1.text() != "" && risk2.text() == "") {
risk2.text(quoteRef);
hdf2.val(checkboxID);
appendHrefWithRisks(linkbutton, href, "risk2=" + quoteRef);
} else if (risk1.text() != "" && risk2.text() != "" && risk3.text() == "") {
risk3.text(quoteRef);
hdf3.val(checkboxID);
appendHrefWithRisks(linkbutton, href, "risk3=" + quoteRef);
} else {
alert("You have already selected 3 quotes. Please uncheck one before adding another.");
}
}
function checkAndRemoveFromRiskSlot(quoteRef, linkbutton, href) {
// get IDs of 3 risk slots
var risk1 = $('#lblQuote1');
var risk2 = $('#lblQuote2');
var risk3 = $('#lblQuote3');
if (risk1.text() === quoteRef) {
risk1.text("");
removeFromHref(linkbutton, "risk1", href);
} else if (risk1.text() !== quoteRef && risk2.text() === quoteRef) {
risk2.text("");
removeFromHref(linkbutton, "risk2", href);
} else if (risk1.text() !== quoteRef && risk2.text() !== quoteRef && risk3.text() === quoteRef) {
risk3.text("");
removeFromHref(linkbutton, "risk3", href);
} else {
alert("You have already selected 3 quotes. Please uncheck one before adding another.");
}
}
function appendHrefWithRisks(linkbutton, href, appendData) {
var newHref = "";
newHref = href + '&' + appendData;
linkbutton.attr('href', newHref);
}
function removeFromHref(linkbutton, risk, href) {
var newHref = "";
if (risk == "risk1") {
newHref = href.replace(/&?((risk1))=[^&]*/gi, "");
} else if (risk == "risk2") {
newHref = href.replace(/&?((risk2))=[^&]*/gi, "");
} else if (risk == "risk3") {
newHref = href.replace(/&?((risk3))=[^&]*/gi, "");
}
linkbutton.attr('href', newHref);
}
When a checked box is checked it call on, function checkAndStoreToAvailableRiskSlot
which in turn calls the function appendHrefWithRisks
.
The href
of the link button is passed through the methods and then appended on the successful addition of a risk to a label.
Hopefully this helps someone else someday :)