Ad

Button Onclick Is Fired When Page Loads

- 1 answer

I am new to javascript and am trying to create a Chrome extension that get recenf nfl scores. I have added two buttons, but they are being clicked as soon as the extension loads. Why is this happening? I have added my EventListeners below.

document.getElementById("reg").addEventListener("click",inter("http://www.nfl.com/liveupdate/scorestrip/ss.xml"));
document.getElementById("post").addEventListener("click",inter("http://www.nfl.com/liveupdate/scorestrip/postseason/ss.xml"));
Ad

Answer

You need to wrap your inter function inside a function block:

document.getElementById("reg").addEventListener("click",function() {inter("http://www.nfl.com/liveupdate/scorestrip/ss.xml")});

Otherwise it is executed while the browser parses it.

Ad
source: stackoverflow.com
Ad