Reputation: 321
I am trying to use windows.addEventListener to execute my function on clicking a div. However nothing is happening.
This is my code:
Javascript:
window.addEventListener('load', init);
function init(){
document.addEventListener('DOMContentLoaded', function() {
document.getElementById("wheelContainer").addEventListener("onclick", function() { winPrize;
}, false);
});
}
Can someone please tell me what am I doing wrong.
Thankyou.
Upvotes: 3
Views: 13336
Reputation: 707328
There are at least three things wrong here:
addEventLisener()
is "click"
, not "onclick"
.load
event OR the DOMContentLoaded
event, but not both.winPrize();
?So, cleaning all of those issues, you would have this:
document.addEventListener('DOMContentLoaded', function() {
document.getElementById("wheelContainer").addEventListener("click", function() { winPrize();
}, false);
});
You don't need the init()
function or the window load event handler at all since you can just install the event handler when the DOMContendLoaded
event fires.
Upvotes: 8