0

I'm trying to assign the "onclick" for a javascript generated <a> element to some function, loadfunction(project[j].getName(), project[j].getPath()).

var projects = []; //Some array of classes in it

function loadProject(aName, aPath){
    //does some stuff
}

var j;
for(j = 0; j < projects.length; j++){
    var div = document.createElement("div");
    var a = document.createElement("a");
    var linkText = document.createTextNode(projects[j]);
    a.appendChild(linkText);
    a.title = projects[j].getName();
    a.href = "AProject.html";
    a.onclick = function(){loadProject(projects[j].getName(), projects[j].getPath());};
    div.appendChild(a)
    div.classList.add("project");
    document.getElementById("projcontainer").appendChild(div)
}

Ideally, this should set the onclick to call loadProject() with a defined parameter. The elements are generated, but they are not assigned any onclick function and I can't figure out why. Any help would be appreciated.

Danila
  • 3
  • 3
  • `projects` is an array - it doesn't have a `getName()` or `getPath()` method. – Scott Marcus Mar 17 '20 at 17:05
  • @ScottMarcus but `projects[j]` would return a class, and this class has defined `getName()` and `getPath()` methods. I don't think this is the issue because everything else in this code works, just the onclick isn't being assigned. – Danila Mar 17 '20 at 18:21

0 Answers0