Does this function have to be nested? Recent JavaScript student here. I ran afoul of some weirdness with event listeners that led to my code being unresponsive. After some reading and tweaking, I arrived at a version that worked in JSFiddle, but not in the browser. I finally got it working in the browser, but now my code looks like this:

window.onload = function () {
    function bgnAddChllng() {
        alert("This is a test alert.");
    }
var addChllng = document.getElementById("addChllng");
addChllng.addEventListener('click', bgnAddChllng, false);
}


But the use of window.onload and nesting the actual useful code in an anonymous function seems bloated/hacky to me. What, if anything, can I do to clean it up?

Available on a JSFiddle here.
window.onload = function () {



Mixing DOM-0 event handlers and addEventListener is inconsistent, pick one and stick with it.

window.addEventListener("load", function () {





function bgnAddChllng() {



Don't disemvowel your variables. This isn't Wheel of Fortune, vowels don't cost anything. 

function beginAdditionChallenge() {





var addChllng = document.getElementById("addChllng");



Use proper indentation. JSFiddle has a handy TidyUp button to do it for you. Consider a more descriptive ID for the button.

var button = document.getElementById("challengeButton");





addChllng.addEventListener('click', bgnAddChllng, false);



Switching between single and double quotes is inconsistent, pick one and stick with it. useCapture defaults to false, there's no need to explicitly pass it. 

button.addEventListener("click", beginAdditionChallenge);





  nesting the actual useful code in an anonymous function seems bloated/hacky to me.


It's a pretty common practice, but if you don't like it, give the function a name and don't nest it, just as you did with the button click event handler.

function beginAdditionChallenge() {
    alert("This is a test alert.");
}

function initChallenge() {
    var button = document.getElementById("challengeButton");
    button.addEventListener("click", beginAdditionChallenge);   
}

window.addEventListener("load", initChallenge);