HTML radio buttons

From: af (CAER)17 Feb 2011 14:05
To: Radio 94 of 95
Um, that seems like a vastly over-complex function just for removing commas from a string.
javascript code:
var deleteCommas = function (num) {
    return (num + "").replace(/,/g, "");
};

But aside from that, you're calling DeleteComma() on the result of a function that generates a list of checkboxes. There are no commas to delete from such a list. Moreover, you'e calling it after the function has returned, so it never actually gets exectuted.

edit:
Looking over the code, the bit you need to change is this loop:
javascript code:
for (i = 0; i < max; i += 1) {
    if (checkBoxes[i].checked) {
        // Get values directly from table cells.
        total += +checkBoxes[i].parentElement.
            previousElementSibling.innerHTML;
        totalWholesale += +checkBoxes[i].parentElement.
            previousElementSibling.previousElementSibling.innerHTML;
    }
}
Change it to this:
javascript code:
var element, deleteCommas;
 
deleteCommas = function (num) {
    return (num + "").replace(/,/g, "");
}; 
 
for (i = 0; i < max; i += 1) {
    if (checkBoxes[i].checked) {
        element = checkBoxes[i].parentElement.previousSibling;
        total += +deleteCommas(element.innerHTML);
 
        element = element.previousSibling;
        totalWholesale += +deleteCommas(element.innerHTML);
    }
}


edit again:
Actually to make it even more resilient, you could change deleteCommas() to:
javascript code:
stripNonNumeric = function (num) {
    return (num + "").replace(/[^0-9.]/g, "");
};
That way it would remove things like currency symbols or extra spaces or whatnot - anything that isn't a digit or a .
EDITED: 17 Feb 2011 14:30 by CAER
From: Radio17 Feb 2011 14:54
To: af (CAER) 95 of 95
Cheers - much appreciated once again!