I’m trying to learn the basics of JavaScript
I cant figure out why this doesn’t work…it’s so basic?
<html>
<head>
<meta charset="utf-8" />
<style type="text/css">
ul{
list-style-type:none;
}
div{
width:300px;
height:200px;
background-color:#0066cc;
}
</style>
<script language="text/javascript">
var testing = document.getElementById("addBtn");
testing.addEventListener("click", function(e){ alert("test") } , false);
</script>
</head>
<body>
<ul id="placeholder"></ul>
<a href="#" id="addBtn">Add</a>
</body>
</html>
The
addBtnis not loaded when you attempt to access it. You should perform that action once the DOM has loaded, either by moving that code to the bottom of the file, or through theonloadevent: