I am new to javascript and am trying to code a click event handler for an html button element with id="display". When I use a window.onload function and add my .onclick event it runs when the window loads and doesn't wait for the click event. I have tried .addEventListener('click', dispResults) method and although the dispResults function doesn't run onload, it doesn't run with the click event either. Please help...
var names = ["Ben", "Joel", "Judy", "Anne"];
var scores = [88, 98, 77, 88];
var $ = function (id) {
return document.getElementById(id);
}
var dispResults = function () {
var avg;
var total = 0;
for (var i = 0; i < scores.length; i++) {
total = total + scores[i];
}
avg = total/scores.length;
$("results").value = avg.toFixed(0);
}
window.onload = function () {
$("display").onclick = dispResults();
}
Thank you.