-1

I have a script that makes that makes a tile for every item in an XML-file. The tile is a div inside an "a" tag created by javascript.

document.getElementById('Body').appendChild(tileLink);
tileLink.appendChild(tile);
tile.appendChild(tileTitle);
tile.appendChild(tileImg);
document.write(" ");

So when tileLink is clicked a javascript function "showDiv(divId)" has to be loaded. divId is a variable in the script needed to load the function. I've tried these 2 lines but with both the script doesn't work and no objects are loaded.

tileLink.onclick = showDiv(divId);
textLink.addEventListener("click", showDiv(divId));

Where am I wrong?

1 Answers1

3

Event handlers require function references. By invoking the function, you are immediately executing it.

textLink.addEventListener("click", function() {
    showDiv(divId);
});
Sterling Archer
  • 22,070
  • 18
  • 81
  • 118