The code has many problems. The addEvent function isn't right (see simplified version), using the wrong variables in the wrong place e.g. incrementing the DOM element not the count variable, checked
should be used not selected
. Your radio buttons aren't grouped and so all three could be checked at once.
var redCount = 0;
var greenCount = 0;
var blueCount = 0;
var redColor;
var greenColor;
var blueColor;
var viewSurvey;
var submitSurvey;
var formSurvey;
function $(id) {
return document.getElementById(id);
}
function showSurvey() {
alert("red: " + redCount + ", green:" + greenCount + ", blue: " + blueCount);
return false;
}
function sendSurvey() {
if((redColor).checked) {
redCount += 1;
} else if((greenColor).checked) {
greenCount += 1;
} else if((blueColor).checked) {
blueCount += 1;
}
return false;
}
function addEvent(elem, event, handler){
elem[event] = handler;
}
window.onload = function(){
redColor = $('red');
greenColor = $('green');
blueColor = $('blue');
viewSurvey = $('viewSurvey');
submitSurvey = $('submitSurvey');
formSurvey = $('surveyForm');
addEvent(viewSurvey, 'onclick', showSurvey);
addEvent(submitSurvey, 'onclick', sendSurvey);
}