I have a function (show below) that totals up all the hours on the page onBlur of the “.total” input fields. This function works perfectly in FF but in Chrome and Safari I am getting this error: SyntaxError: Unexpected EOF —I did some research but I am lost as to what might be causing this.
thanks for any help!
function totalUpHours() {
//add all values to an array
var hrsTotalsarr = new Array();
$('.total').each(function() {
var thisTotal = $(this).val();
//clean the value
if (thisTotal == "HRS") {
thisTotal = 0;
}
hrsTotalsarr.push(thisTotal);
});
//sum up the array
var hrsTotal = eval(hrsTotalsarr.join('+'));
$('#taskHRStotals').val(hrsTotal);
}
Sometimes invalid characters can work their way into code when you copy/paste code from websites.
Unfortunately, they’re often invisible, and can only be noticed if you move your cursor across it with the arrow keys on your keyboard. When you get to the invalid one, the cursor will pause for a single keystroke.
Aside from that, consider this alternative to using
eval()in your code.This is called
map/reduce, and is a very handy pattern to know.