1

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>
Coops
  • 13
  • 3

1 Answers1

1

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

c7x43t
  • 274
  • 2
  • 6
  • Thank you soooo much! Really appreciate your help! – Coops Nov 10 '18 at 11:57
  • And thanks for the feedback on enrolArray (I created it trying to bypass the issue I was having with onclick and forgot to delete) and also fixed up the pos. – Coops Nov 10 '18 at 11:58