Reputation: 35
I have a series of checkboxes, check to see that they've been checked, and if they have, add a string to an array. Then I loop through the array of strings and add the strings in the array to a new string separated by commas. Unfortunately, this always results in errors. In Chrome, the console reads "Invalid String Length". In Firefox, the error reads "allocation size overflow".
JavaScript:
// Compile List of Interests
interests = new Array();
if ( $("input[name=interestRoofing]").is(':checked') == true ) interests.push("Roofing");
if ( $("input[name=interestSiding]").is(':checked') == true ) interests.push("Siding");
if ( $("input[name=interestGutters]").is(':checked') == true ) interests.push("Gutters");
if ( $("input[name=interestWindows]").is(':checked') == true ) interests.push("Windows");
if ( $("input[name=interestPaint]").is(':checked') == true ) interests.push("Exterior Paint");
interestsString = "";
for ( i = 0; i < interests.length; i++ ) { // "Invalid String Length" in Chrome on This Line
interestsString = interestsString + interests[i]; // "Allocation Size Overload" in Firefox on This Line
if ( i < interests.length - 2 ) interestsString += ", ";
if ( i = interests.length - 2 ) interestsString += "and ";
}
HTML:
<p>Interested In:</p>
<p><input type="checkbox" name="interestRoofing" value="">Roofing</p>
<p><input type="checkbox" name="interestSiding" value="">Siding</p>
<p><input type="checkbox" name="interestGutters" value="">Gutters</p>
<p><input type="checkbox" name="interestWindows" value="">Windows</p>
<p><input type="checkbox" name="interestPaint" value="">Exterior Paint</p>
I'm sure that whatever is going wrong will just hit me in the face when I see it, but I've been sitting on this problem all day and haven't come up with a solution yet. Obviously there's something here I'm not seeing. Any help would be much appreciated. Strangely, the console in Internet Explorer doesn't spit anything out, as if nothing went wrong. I can rid Chrome of the Invalid String Length error by adding an if statement to the beginning of the for loop checking to ensure that the length of the array is not 0. However, the error in Firefox remains, so that must not be the solution.
Upvotes: 2
Views: 6984
Reputation: 2274
if ( i = interests.length - 2 ) interestsString += "and ";
You are doing an assignment here. You probably mean to be comparing.
Upvotes: 5