Search code examples
javascripthtmlonclickgetelementbyid

onclick event ocurring without click event actually happening - js


Folks I'm a complete novice so apologise upfront for what is going to be a very basic question, I'm sure.

I have a javascript file that is linked to a html file which contains a number of buttons. The onclick event for one such button seems to be completely bypassing the onlick event and triggering as soon as the page opens. Any ideas? Sorry to ask what is likely something really basic, but I have lost count of the hours I have spent trying to get it to work only on the "click" event....

in java:

var arrEnrolments=[];//global array
function init() {
    populateEnrolments();
    var enrolArray=arrEnrolments

    document.getElementById("resetEnrol").onclick=resetEnrolments;
    document.getElementById("listEnrol").onclick=listAllEnrols;
   document.getElementById("sortEnrol").onclick=sortByName(arrEnrolments); 
}

function populateEnrolments(){
arrEnrolments=[];
var stuOne={id:1234567, name: "Jones, Brigit", mark: null, grade: ""};
var stuTwo={id:1234566, name: "Somers, Tom ", mark: null, grade: ""};
var stuThree={id:1222445, name: "Lee, Vera", mark: null, grade: ""};
var stuFour={id:1220123, name: "Crawford, Anne", mark: null, grade: ""};
var stuFive={id:1244444, name: "Wu, Russel", mark: null, grade: ""};
var stuSix={id:1238899, name: "Shaw, Boris", mark: null, grade: ""};
var stuSeven={id:1237799, name: "Wilson, Terri", mark: null, grade: ""};
arrEnrolments=[stuOne, stuTwo, stuThree, stuFour, stuFive, stuSix, stuSeven];   
}

function sortByName(searchArray) {

var pos=0;
var count, temp;

do{
    var msgOut="Student records sorted into Name ascending sequence";
    count=0;
    for(var pos=0; pos<searchArray.length-1; pos++){
        if(searchArray[pos].name>searchArray[pos+1].name){
            temp=searchArray[pos];
            searchArray[pos]=searchArray[pos+1];
            searchArray[pos+1]=temp;
            count++
        }//end if
    }//end for
} while (count>0)//end do

addToOutput(msgOut);
}

function addToOutput(output){
var msgOut=output;
document.getElementById("output").innerHTML=msgOut;

}

window.onload = init;

In html:

<body>
<header>
<h1>Tracking Student Enrolments and Completions<h1>
</header>
<article class="flex-container">
<section class="flex-item"> 
<h2>Enrolments</h2>
<button type="button" id="resetEnrol">Reset Enrolment List</button>
<button type="button" id="listEnrol">List All Enrolments</button>
<button type="button" id="sortEnrol">Sort Enrolments by Name</button><br/>
</article>
</body>

Solution

  • The solution:

    document.getElementById('sortEnrol').onclick = sortByName(arrEnrolments);

    is not registering an eventListener but instead executing the sortByName function. You need a closure here as follows:

    document.getElementById('sortEnrol').onclick = function () { sortByName(arrEnrolments); };

    A few comments:

    • enrolArray variable is created in the init function but never used

    • pos variable is defined twice in the sortByName function